

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>agpy.showspec &mdash; agpy 0.1 documentation</title>
    
    <link rel="stylesheet" href="../../_static/agogo.css" type="text/css" />
    <link rel="stylesheet" href="../../_static/pygments.css" type="text/css" />
    
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../../',
        VERSION:     '0.1',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="../../_static/jquery.js"></script>
    <script type="text/javascript" src="../../_static/underscore.js"></script>
    <script type="text/javascript" src="../../_static/doctools.js"></script>
    <link rel="top" title="agpy 0.1 documentation" href="../../index.html" />
    <link rel="up" title="agpy" href="../agpy.html" />
     
    <script type="text/javascript">

      var _gaq = _gaq || [];
      _gaq.push(['_setDomainName', 'pyspeckit.bitbucket.org']);
      _gaq.push(['_setAllowHash', false]);
      _gaq.push(['_trackPageview']);


    </script>
  </head>
  <body>
    <div class="header-wrapper">
      <div class="header">
        <h1><a href="../../index.html">agpy 0.1 documentation</a></h1>
        <div class="rel">
          <a href="http://agpy.googlecode.com">agpy Home </a>  |
          <a href=../../index.html>Docs Home </a>  |
          <a href="http://code.google.com/p/agpy/w/list">Wiki</a>  |
          <a href=../../search.html>Search </a>
        </div>
       </div>
    </div>

    <div class="content-wrapper">
      <div class="content">
        <div class="document">
            
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body">
            
  <h1>Source code for agpy.showspec</h1><div class="highlight"><pre>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd">showspec is my homegrown spectrum plotter, meant to somewhat follow STARLINK&#39;s</span>
<span class="sd">SPLAT and have functionality similar to GAIA, but with an emphasis on producing</span>
<span class="sd">publication-quality plots (which, while splat may do, it does unreproducibly)</span>


<span class="sd">.. todo::</span>
<span class="sd">    -add spectrum arithmetic tools</span>
<span class="sd">        (as is, you can use numpy.interp with sp.vind and sp.spectrum pretty</span>
<span class="sd">        easily)</span>
<span class="sd">    -implement other fitters</span>
<span class="sd">        -e.g., NH3 hyperfine, Voigt</span>
<span class="sd">    -move to object-oriented pylab/pyplot implementation (for bulk / speedup work)</span>
<span class="sd">    -allow for non-plotting fitting work (curious... I&#39;ve never needed that yet)</span>
<span class="sd">    -Equivalent Width measurement without gaussian fitting</span>
<span class="sd">        -probably should be part of the baseline code</span>
<span class="sd">    -write documentation other people can read</span>

<span class="sd">12/21/2011 - ALL of the above to do is IS DONE!  It&#39;s now hosted at &lt;http://pyspeckit.bitbucket.org&gt;</span>

<span class="sd">&quot;&quot;&quot;</span>



<span class="kn">import</span> <span class="nn">math</span>

<span class="kn">import</span> <span class="nn">pylab</span>
<span class="kn">from</span> <span class="nn">pylab</span> <span class="kn">import</span> <span class="o">*</span>
<span class="kn">import</span> <span class="nn">matplotlib</span>

<span class="kn">from</span> <span class="nn">mpfit</span> <span class="kn">import</span> <span class="n">mpfit</span>

<span class="kn">from</span> <span class="nn">collapse_gaussfit</span> <span class="kn">import</span> <span class="o">*</span>
<span class="kn">from</span> <span class="nn">ratosexagesimal</span> <span class="kn">import</span> <span class="o">*</span>
<span class="kn">import</span> <span class="nn">pyfits</span>
<span class="kn">import</span> <span class="nn">gaussfitter</span>

<span class="kn">import</span> <span class="nn">numpy</span>
<span class="kn">from</span> <span class="nn">numpy</span> <span class="kn">import</span> <span class="n">isnan</span>
<span class="kn">from</span> <span class="nn">mad</span> <span class="kn">import</span> <span class="n">MAD</span><span class="p">,</span><span class="n">nanmedian</span>

<div class="viewcode-block" id="steppify"><a class="viewcode-back" href="../../agpy.html#agpy.showspec.steppify">[docs]</a><span class="k">def</span> <span class="nf">steppify</span><span class="p">(</span><span class="n">arr</span><span class="p">,</span><span class="n">isX</span><span class="o">=</span><span class="bp">False</span><span class="p">,</span><span class="n">interval</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span><span class="n">sign</span><span class="o">=+</span><span class="mf">1.0</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    *support function*</span>
<span class="sd">    Converts an array to double-length for step plotting</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">if</span> <span class="n">isX</span> <span class="ow">and</span> <span class="n">interval</span><span class="o">==</span><span class="mi">0</span><span class="p">:</span>
        <span class="n">interval</span> <span class="o">=</span> <span class="n">numpy</span><span class="o">.</span><span class="n">abs</span><span class="p">(</span><span class="n">arr</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span><span class="o">-</span><span class="n">arr</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span> <span class="o">/</span> <span class="mf">2.0</span>
    <span class="n">newarr</span> <span class="o">=</span> <span class="n">pylab</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="nb">zip</span><span class="p">(</span><span class="n">arr</span><span class="o">-</span><span class="n">sign</span><span class="o">*</span><span class="n">interval</span><span class="p">,</span><span class="n">arr</span><span class="o">+</span><span class="n">sign</span><span class="o">*</span><span class="n">interval</span><span class="p">))</span><span class="o">.</span><span class="n">ravel</span><span class="p">()</span>
    <span class="k">return</span> <span class="n">newarr</span>
</div>
<div class="viewcode-block" id="SpecPlotter"><a class="viewcode-back" href="../../agpy.html#agpy.showspec.SpecPlotter">[docs]</a><span class="k">class</span> <span class="nc">SpecPlotter</span><span class="p">:</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    SpecPlotter class.  Takes in a spectrum or data cube, plotting properties,</span>
<span class="sd">    and a velocity axis determination function.  Look at splat_1d for a wrapper</span>
<span class="sd">    that might actually be useful.</span>

<span class="sd">    Whew, needs more documentation</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span>  <span class="n">cube</span><span class="p">,</span>  <span class="n">axis</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span>  <span class="n">xtol</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span>  <span class="n">ytol</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="n">vconv</span><span class="o">=</span><span class="k">lambda</span>
            <span class="n">x</span><span class="p">:</span> <span class="n">x</span><span class="p">,</span> <span class="n">xtora</span><span class="o">=</span><span class="k">lambda</span> <span class="n">x</span><span class="p">:</span> <span class="n">x</span><span class="p">,</span> <span class="n">ytodec</span><span class="o">=</span><span class="k">lambda</span> <span class="n">x</span><span class="p">:</span> <span class="n">x</span><span class="p">,</span> <span class="n">specname</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span>
            <span class="n">dv</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="n">color</span><span class="o">=</span><span class="s">&#39;k&#39;</span><span class="p">,</span> <span class="n">hdr</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="n">errspec</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span>  <span class="n">maskspec</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span>
            <span class="n">fig</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="n">fignum</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="n">clear</span><span class="o">=</span><span class="bp">True</span><span class="p">,</span> <span class="n">title</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="n">xunits</span><span class="o">=</span><span class="s">&#39;km/s&#39;</span><span class="p">,</span>
            <span class="n">erralpha</span><span class="o">=</span><span class="mf">0.2</span><span class="p">,</span> <span class="n">ivconv</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="n">autorefresh</span><span class="o">=</span><span class="bp">True</span><span class="p">,</span> <span class="n">reffreq</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span>
            <span class="n">gaiafignum</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span> <span class="n">gaiafig</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="n">clickid</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span> <span class="p">):</span>
      <span class="bp">self</span><span class="o">.</span><span class="n">vconv</span> <span class="o">=</span> <span class="n">vconv</span>
      <span class="bp">self</span><span class="o">.</span><span class="n">xtora</span> <span class="o">=</span> <span class="n">xtora</span>
      <span class="bp">self</span><span class="o">.</span><span class="n">ytodec</span> <span class="o">=</span> <span class="n">ytodec</span>
      <span class="bp">self</span><span class="o">.</span><span class="n">cube</span> <span class="o">=</span> <span class="n">cube</span> <span class="c"># where(numpy.isnan(cube),0,cube)</span>
      <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">cube</span><span class="o">.</span><span class="n">shape</span><span class="p">)</span> <span class="o">&gt;</span> <span class="mi">1</span><span class="p">:</span>
          <span class="bp">self</span><span class="o">.</span><span class="n">spectrum</span> <span class="o">=</span> <span class="n">cube</span><span class="p">[:,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">]</span> <span class="c"># spectrum is what&#39;s plotted; cube is the &quot;raw data&quot;</span>
      <span class="k">else</span><span class="p">:</span>
          <span class="bp">self</span><span class="o">.</span><span class="n">spectrum</span> <span class="o">=</span> <span class="n">cube</span> <span class="c"># spectrum is what&#39;s plotted; cube is the &quot;raw data&quot;</span>
      <span class="bp">self</span><span class="o">.</span><span class="n">specname</span><span class="o">=</span><span class="n">specname</span>
      <span class="bp">self</span><span class="o">.</span><span class="n">dv</span><span class="o">=</span><span class="n">dv</span>
      <span class="bp">self</span><span class="o">.</span><span class="n">reffreq</span><span class="o">=</span><span class="n">reffreq</span>
      <span class="bp">self</span><span class="o">.</span><span class="n">scale</span><span class="o">=</span><span class="mf">1.0</span>
      <span class="bp">self</span><span class="o">.</span><span class="n">units</span><span class="o">=</span><span class="s">&#39;K&#39;</span>
      <span class="bp">self</span><span class="o">.</span><span class="n">xunits</span><span class="o">=</span><span class="n">xunits</span>
      <span class="bp">self</span><span class="o">.</span><span class="n">voff</span><span class="o">=</span><span class="mf">0.0</span>
      <span class="bp">self</span><span class="o">.</span><span class="n">offset</span><span class="o">=</span><span class="mf">0.0</span>
      <span class="bp">self</span><span class="o">.</span><span class="n">continuum</span><span class="o">=</span><span class="mf">0.0</span>
      <span class="bp">self</span><span class="o">.</span><span class="n">errspec</span> <span class="o">=</span> <span class="n">errspec</span>
      <span class="bp">self</span><span class="o">.</span><span class="n">erralpha</span><span class="o">=</span><span class="n">erralpha</span>
      <span class="bp">self</span><span class="o">.</span><span class="n">plotcolor</span><span class="o">=</span><span class="n">color</span>
      <span class="bp">self</span><span class="o">.</span><span class="n">specfit</span> <span class="o">=</span> <span class="n">Specfit</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span>
      <span class="bp">self</span><span class="o">.</span><span class="n">fitspec</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">specfit</span>
      <span class="bp">self</span><span class="o">.</span><span class="n">baseline</span> <span class="o">=</span> <span class="n">Baseline</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span>
      <span class="c">#self.fft = FFT(self)</span>
      <span class="c">#self.psd = self.fft.psd</span>
      <span class="bp">self</span><span class="o">.</span><span class="n">vmin</span><span class="o">=</span><span class="bp">None</span>
      <span class="bp">self</span><span class="o">.</span><span class="n">vmax</span><span class="o">=</span><span class="bp">None</span>
      <span class="bp">self</span><span class="o">.</span><span class="n">title</span><span class="o">=</span><span class="n">title</span>
      <span class="bp">self</span><span class="o">.</span><span class="n">ivconv</span><span class="o">=</span><span class="n">ivconv</span>
      <span class="bp">self</span><span class="o">.</span><span class="n">autorefresh</span><span class="o">=</span><span class="n">autorefresh</span>
      <span class="bp">self</span><span class="o">.</span><span class="n">spectrumplot</span><span class="o">=</span><span class="bp">None</span>
      <span class="bp">self</span><span class="o">.</span><span class="n">errorplot</span><span class="o">=</span><span class="bp">None</span>
      <span class="bp">self</span><span class="o">.</span><span class="n">gaiafignum</span> <span class="o">=</span> <span class="n">gaiafignum</span>
      <span class="bp">self</span><span class="o">.</span><span class="n">gaiafig</span> <span class="o">=</span> <span class="n">gaiafig</span>
      <span class="bp">self</span><span class="o">.</span><span class="n">clickid</span> <span class="o">=</span> <span class="n">clickid</span>
      <span class="bp">self</span><span class="o">.</span><span class="n">plotkwargs</span> <span class="o">=</span> <span class="n">kwargs</span>
      <span class="k">if</span> <span class="n">maskspec</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">None</span><span class="p">:</span>
          <span class="bp">self</span><span class="o">.</span><span class="n">maskspec</span> <span class="o">=</span> <span class="n">maskspec</span>
      <span class="k">else</span><span class="p">:</span>
          <span class="bp">self</span><span class="o">.</span><span class="n">maskspec</span> <span class="o">=</span> <span class="n">zeros</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">cube</span><span class="o">.</span><span class="n">shape</span><span class="p">)</span>
      <span class="bp">self</span><span class="o">.</span><span class="n">linecollections</span> <span class="o">=</span><span class="p">[]</span>
      <span class="bp">self</span><span class="o">.</span><span class="n">texts</span> <span class="o">=</span><span class="p">[]</span>
      <span class="k">if</span> <span class="n">hdr</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">header</span> <span class="o">=</span> <span class="n">hdr</span>
  
      <span class="c"># figure out where to put the plot</span>
      <span class="k">if</span> <span class="n">fig</span> <span class="ow">is</span> <span class="bp">None</span> <span class="ow">and</span> <span class="n">axis</span> <span class="ow">is</span> <span class="bp">None</span><span class="p">:</span>
          <span class="n">fig</span><span class="o">=</span><span class="n">figure</span><span class="p">(</span><span class="n">fignum</span><span class="p">)</span>
          <span class="k">if</span> <span class="n">clear</span><span class="p">:</span> <span class="n">fig</span><span class="o">.</span><span class="n">clf</span><span class="p">()</span>
          <span class="bp">self</span><span class="o">.</span><span class="n">axis</span> <span class="o">=</span> <span class="n">fig</span><span class="o">.</span><span class="n">gca</span><span class="p">()</span>
      <span class="k">elif</span> <span class="n">fig</span> <span class="ow">is</span> <span class="bp">None</span> <span class="ow">and</span> <span class="n">axis</span> <span class="ow">is</span> <span class="bp">None</span><span class="p">:</span>
          <span class="bp">self</span><span class="o">.</span><span class="n">axis</span> <span class="o">=</span> <span class="n">pylab</span><span class="o">.</span><span class="n">gca</span><span class="p">()</span>
      <span class="k">elif</span> <span class="n">fig</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">None</span> <span class="ow">and</span> <span class="n">axis</span> <span class="ow">is</span> <span class="bp">None</span><span class="p">:</span>
          <span class="k">if</span> <span class="n">clear</span><span class="p">:</span> <span class="n">fig</span><span class="o">.</span><span class="n">clf</span><span class="p">()</span>
          <span class="bp">self</span><span class="o">.</span><span class="n">axis</span> <span class="o">=</span> <span class="n">fig</span><span class="o">.</span><span class="n">gca</span><span class="p">()</span>
      <span class="k">elif</span> <span class="n">fig</span> <span class="ow">is</span> <span class="bp">None</span> <span class="ow">and</span> <span class="n">axis</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">None</span><span class="p">:</span>
          <span class="bp">self</span><span class="o">.</span><span class="n">axis</span> <span class="o">=</span> <span class="n">axis</span>
      <span class="k">else</span><span class="p">:</span> <span class="c"># if figure and axis are both set, just use axis</span>
          <span class="bp">self</span><span class="o">.</span><span class="n">axis</span> <span class="o">=</span> <span class="n">axis</span>
      <span class="k">if</span> <span class="n">clear</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">axis</span><span class="o">.</span><span class="n">clear</span><span class="p">()</span>
  
    <span class="k">def</span> <span class="nf">__call__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">event</span><span class="p">):</span>
      <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">      Connects map cube to specplotter...</span>
<span class="sd">      &quot;&quot;&quot;</span>
      <span class="k">if</span> <span class="n">event</span><span class="o">.</span><span class="n">inaxes</span><span class="p">:</span>
        <span class="n">clickX</span> <span class="o">=</span> <span class="n">event</span><span class="o">.</span><span class="n">xdata</span>
        <span class="n">clickY</span> <span class="o">=</span> <span class="n">event</span><span class="o">.</span><span class="n">ydata</span>
        <span class="n">tb</span> <span class="o">=</span> <span class="n">get_current_fig_manager</span><span class="p">()</span><span class="o">.</span><span class="n">toolbar</span>
        <span class="c">#if ((self.axis is None) or (self.axis==event.inaxes)) and tb.mode==&#39;&#39;:</span>
        <span class="k">if</span> <span class="n">event</span><span class="o">.</span><span class="n">button</span><span class="o">==</span><span class="mi">1</span> <span class="ow">and</span> <span class="n">tb</span><span class="o">.</span><span class="n">mode</span><span class="o">==</span><span class="s">&#39;&#39;</span><span class="p">:</span>
            <span class="k">print</span> <span class="s">&quot;OverPlotting spectrum from point </span><span class="si">%i</span><span class="s">,</span><span class="si">%i</span><span class="s">&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="n">clickX</span><span class="p">,</span><span class="n">clickY</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">plotspec</span><span class="p">(</span><span class="n">clickY</span><span class="p">,</span><span class="n">clickX</span><span class="p">,</span><span class="n">button</span><span class="o">=</span><span class="n">event</span><span class="o">.</span><span class="n">button</span><span class="p">,</span><span class="n">cube</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
        <span class="k">elif</span> <span class="n">event</span><span class="o">.</span><span class="n">button</span><span class="o">==</span><span class="mi">2</span><span class="p">:</span>
            <span class="k">print</span> <span class="s">&quot;Plotting spectrum from point </span><span class="si">%i</span><span class="s">,</span><span class="si">%i</span><span class="s">&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="n">clickX</span><span class="p">,</span><span class="n">clickY</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">plotspec</span><span class="p">(</span><span class="n">clickY</span><span class="p">,</span><span class="n">clickX</span><span class="p">,</span><span class="n">button</span><span class="o">=</span><span class="n">event</span><span class="o">.</span><span class="n">button</span><span class="p">,</span><span class="n">cube</span><span class="o">=</span><span class="bp">True</span><span class="p">,</span><span class="n">clear</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
        <span class="k">elif</span> <span class="n">event</span><span class="o">.</span><span class="n">button</span><span class="o">==</span><span class="mi">3</span><span class="p">:</span>
            <span class="k">print</span> <span class="s">&quot;Disconnecting GAIA-like tool&quot;</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">gaiafig</span><span class="o">.</span><span class="n">canvas</span><span class="o">.</span><span class="n">mpl_disconnect</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">clickid</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">print</span> <span class="s">&quot;Call failed for some reason: &quot;</span>
            <span class="k">print</span> <span class="s">&quot;event: &quot;</span><span class="p">,</span><span class="n">event</span>
  
<div class="viewcode-block" id="SpecPlotter.plotspec"><a class="viewcode-back" href="../../agpy.html#agpy.showspec.SpecPlotter.plotspec">[docs]</a>    <span class="k">def</span> <span class="nf">plotspec</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">i</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span> <span class="n">j</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span> <span class="n">cube</span><span class="o">=</span><span class="bp">False</span><span class="p">,</span> <span class="n">title</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span>
            <span class="n">clear</span><span class="o">=</span><span class="bp">False</span><span class="p">,</span> <span class="n">color</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="n">continuum</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span>
            <span class="n">axis</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="n">offset</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="n">scale</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="n">voff</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="n">vmin</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span>
            <span class="n">vmax</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="n">units</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="n">xunits</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="n">erralpha</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="n">plotpix</span><span class="o">=</span><span class="bp">False</span><span class="p">,</span>
            <span class="n">errstyle</span><span class="o">=</span><span class="s">&#39;fill&#39;</span><span class="p">,</span> <span class="n">autorefresh</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="n">button</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
      <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">      Plot a spectrum</span>
<span class="sd">      Originally written to plot spectra from data cubes, hence the i,j parameter</span>
<span class="sd">      to specify the location in the cube</span>

<span class="sd">      Now, cube defaults to False, but you can still pass in a data cube.</span>

<span class="sd">      Inputs:</span>
<span class="sd">        title,color, kwargs - semi-obvious plot-related comands</span>
<span class="sd">        axis - You can pass in a Matplotlib axis instance and it will plot on that</span>
<span class="sd">        clear - Clear the axis before plotting?</span>
<span class="sd">        continuum - if you&#39;ve already subtracted out a continuum, you can add</span>
<span class="sd">            it back in (only if it is a constant offset).  It will be included in </span>
<span class="sd">            the spectrum</span>
<span class="sd">        offset - Like continuum, but ONLY for plotting purposes.  Will move the </span>
<span class="sd">            plot vertically but will NOT include values in the .spectrum </span>
<span class="sd">        scale - multiplicative factor to scale the data by (NOT for plotting</span>
<span class="sd">            purposes; modifies spectrum)</span>
<span class="sd">        voff - Shift the spectrum on the velocity axis by this amount</span>
<span class="sd">        vmin,vmax - only plot within this range</span>
<span class="sd">            (note that these keywords passed to splat_1d MAY crop the spectrum)</span>
<span class="sd">        units - units of the data.  At the moment, no conversions are done</span>
<span class="sd">        xunits - units of the Y axis.  Can affect other procedures, like show_lines,</span>
<span class="sd">            and some unit conversion (Hz to GHz) is done</span>
<span class="sd">        erralpha - Transparency of the errorbars if plotted</span>
<span class="sd">        errstyle - style of errorbars if plotted</span>
<span class="sd">        plotpix - if set, will plot against a pixel (channel) axis instead of a</span>
<span class="sd">            physical axis</span>
<span class="sd">        autorefresh - automatically update the plot when fitting gaussians, labeling,</span>
<span class="sd">            etc?</span>
<span class="sd">      &quot;&quot;&quot;</span>
  
      <span class="k">if</span> <span class="n">kwargs</span><span class="o">.</span><span class="n">has_key</span><span class="p">(</span><span class="s">&#39;fignum&#39;</span><span class="p">):</span> <span class="n">kwargs</span><span class="o">.</span><span class="n">pop</span><span class="p">(</span><span class="s">&#39;fignum&#39;</span><span class="p">)</span>  <span class="c"># HACK because I want __init__ to accept different kwargs</span>
      <span class="k">if</span> <span class="n">kwargs</span><span class="o">.</span><span class="n">has_key</span><span class="p">(</span><span class="s">&#39;fig&#39;</span><span class="p">):</span> <span class="n">kwargs</span><span class="o">.</span><span class="n">pop</span><span class="p">(</span><span class="s">&#39;fig&#39;</span><span class="p">)</span>        <span class="c"># is there a better workaround?</span>
      <span class="k">if</span> <span class="n">scale</span>  <span class="ow">is</span> <span class="ow">not</span> <span class="bp">None</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">scale</span> <span class="o">=</span> <span class="n">scale</span>
      <span class="k">if</span> <span class="n">units</span>  <span class="ow">is</span> <span class="ow">not</span> <span class="bp">None</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">units</span> <span class="o">=</span> <span class="n">units</span>
      <span class="k">if</span> <span class="n">xunits</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">None</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">xunits</span><span class="o">=</span> <span class="n">xunits</span>
      <span class="k">if</span> <span class="n">voff</span>   <span class="ow">is</span> <span class="ow">not</span> <span class="bp">None</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">voff</span>  <span class="o">=</span> <span class="n">voff</span>
      <span class="k">if</span> <span class="n">offset</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">None</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">offset</span><span class="o">=</span> <span class="n">offset</span>
      <span class="k">if</span> <span class="n">continuum</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">None</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">continuum</span><span class="o">=</span> <span class="n">continuum</span>
      <span class="k">if</span> <span class="n">color</span>  <span class="ow">is</span> <span class="ow">not</span> <span class="bp">None</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">plotcolor</span><span class="o">=</span><span class="n">color</span>
      <span class="k">if</span> <span class="n">erralpha</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">None</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">erralpha</span><span class="o">=</span> <span class="n">erralpha</span>
      <span class="k">if</span> <span class="n">vmax</span>  <span class="ow">is</span> <span class="ow">not</span> <span class="bp">None</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">vmax</span> <span class="o">=</span> <span class="n">vmax</span>
      <span class="k">if</span> <span class="n">vmin</span>  <span class="ow">is</span> <span class="ow">not</span> <span class="bp">None</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">vmin</span> <span class="o">=</span> <span class="n">vmin</span>
      <span class="k">if</span> <span class="n">title</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">None</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">title</span> <span class="o">=</span> <span class="n">title</span>
      <span class="k">if</span> <span class="n">autorefresh</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">None</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">autorefresh</span> <span class="o">=</span> <span class="n">autorefresh</span>
      <span class="k">if</span> <span class="n">axis</span> <span class="ow">is</span> <span class="bp">None</span><span class="p">:</span> <span class="n">axis</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">axis</span> <span class="c"># allow spectrum to be plotted on other axis</span>
      <span class="k">if</span> <span class="n">clear</span><span class="p">:</span> <span class="n">axis</span><span class="o">.</span><span class="n">clear</span><span class="p">()</span>
  
      <span class="k">if</span> <span class="n">plotpix</span><span class="p">:</span>
          <span class="bp">self</span><span class="o">.</span><span class="n">vind</span> <span class="o">=</span> <span class="n">arange</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">cube</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span>
      <span class="k">else</span><span class="p">:</span>
          <span class="bp">self</span><span class="o">.</span><span class="n">vind</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">vconv</span><span class="p">(</span><span class="n">arange</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">cube</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">0</span><span class="p">]))</span> <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">voff</span>
  
      <span class="k">if</span> <span class="n">kwargs</span><span class="o">.</span><span class="n">has_key</span><span class="p">(</span><span class="s">&#39;fignum&#39;</span><span class="p">):</span> <span class="n">kwargs</span><span class="o">.</span><span class="n">pop</span><span class="p">(</span><span class="s">&#39;fignum&#39;</span><span class="p">)</span>
      <span class="k">if</span> <span class="n">kwargs</span><span class="o">.</span><span class="n">has_key</span><span class="p">(</span><span class="s">&#39;linewidth&#39;</span><span class="p">):</span>
          <span class="n">linewidth</span> <span class="o">=</span> <span class="n">kwargs</span><span class="o">.</span><span class="n">pop</span><span class="p">(</span><span class="s">&#39;linewidth&#39;</span><span class="p">)</span>
      <span class="k">else</span><span class="p">:</span>
          <span class="n">linewidth</span><span class="o">=</span><span class="mf">0.5</span>
  
      <span class="k">if</span> <span class="n">cube</span> <span class="ow">or</span> <span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">cube</span><span class="o">.</span><span class="n">shape</span><span class="p">)</span> <span class="o">==</span> <span class="mi">3</span><span class="p">:</span>
          <span class="bp">self</span><span class="o">.</span><span class="n">spectrum</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">cube</span><span class="p">[:,</span><span class="n">i</span><span class="p">,</span><span class="n">j</span><span class="p">]</span><span class="o">*</span><span class="bp">self</span><span class="o">.</span><span class="n">scale</span><span class="o">+</span><span class="bp">self</span><span class="o">.</span><span class="n">continuum</span><span class="o">-</span><span class="bp">self</span><span class="o">.</span><span class="n">baseline</span><span class="o">.</span><span class="n">basespec</span>
          <span class="bp">self</span><span class="o">.</span><span class="n">spectrumplot</span> <span class="o">=</span> <span class="n">axis</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">vind</span><span class="p">,</span><span class="bp">self</span><span class="o">.</span><span class="n">spectrum</span><span class="o">+</span><span class="bp">self</span><span class="o">.</span><span class="n">offset</span><span class="p">,</span><span class="n">color</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">plotcolor</span><span class="p">,</span>
                  <span class="n">linestyle</span><span class="o">=</span><span class="s">&#39;steps-mid&#39;</span><span class="p">,</span><span class="n">linewidth</span><span class="o">=</span><span class="n">linewidth</span><span class="p">,</span>
                  <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
      <span class="k">else</span><span class="p">:</span>
          <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">maskspec</span><span class="o">.</span><span class="n">sum</span><span class="p">()</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">:</span>
              <span class="n">nanmask</span> <span class="o">=</span> <span class="n">where</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">maskspec</span><span class="p">,</span><span class="n">numpy</span><span class="o">.</span><span class="n">nan</span><span class="p">,</span><span class="mi">1</span><span class="p">)</span>
              <span class="bp">self</span><span class="o">.</span><span class="n">spectrum</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">cube</span><span class="o">*</span><span class="bp">self</span><span class="o">.</span><span class="n">scale</span><span class="o">*</span><span class="n">nanmask</span><span class="o">+</span><span class="bp">self</span><span class="o">.</span><span class="n">continuum</span><span class="o">-</span><span class="bp">self</span><span class="o">.</span><span class="n">baseline</span><span class="o">.</span><span class="n">basespec</span>
              <span class="bp">self</span><span class="o">.</span><span class="n">spectrumplot</span> <span class="o">=</span> <span class="n">axis</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">vind</span><span class="p">,</span><span class="bp">self</span><span class="o">.</span><span class="n">spectrum</span><span class="o">+</span><span class="bp">self</span><span class="o">.</span><span class="n">offset</span><span class="p">,</span><span class="n">color</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">plotcolor</span><span class="p">,</span>
                      <span class="n">linestyle</span><span class="o">=</span><span class="s">&#39;steps-mid&#39;</span><span class="p">,</span><span class="n">linewidth</span><span class="o">=</span><span class="n">linewidth</span><span class="p">,</span>
                      <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
          <span class="k">else</span><span class="p">:</span>
              <span class="bp">self</span><span class="o">.</span><span class="n">spectrum</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">cube</span><span class="o">*</span><span class="bp">self</span><span class="o">.</span><span class="n">scale</span><span class="o">+</span><span class="bp">self</span><span class="o">.</span><span class="n">continuum</span><span class="o">-</span><span class="bp">self</span><span class="o">.</span><span class="n">baseline</span><span class="o">.</span><span class="n">basespec</span>
              <span class="bp">self</span><span class="o">.</span><span class="n">spectrumplot</span> <span class="o">=</span> <span class="n">axis</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">vind</span><span class="p">,</span><span class="bp">self</span><span class="o">.</span><span class="n">spectrum</span><span class="o">+</span><span class="bp">self</span><span class="o">.</span><span class="n">offset</span><span class="p">,</span><span class="n">color</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">plotcolor</span><span class="p">,</span>
                      <span class="n">linestyle</span><span class="o">=</span><span class="s">&#39;steps-mid&#39;</span><span class="p">,</span><span class="n">linewidth</span><span class="o">=</span><span class="n">linewidth</span><span class="p">,</span>
                      <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
          <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">errspec</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">None</span><span class="p">:</span>
              <span class="k">if</span> <span class="n">errstyle</span> <span class="o">==</span> <span class="s">&#39;fill&#39;</span><span class="p">:</span>
                  <span class="bp">self</span><span class="o">.</span><span class="n">errorplot</span> <span class="o">=</span> <span class="p">[</span><span class="n">axis</span><span class="o">.</span><span class="n">fill_between</span><span class="p">(</span><span class="n">steppify</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">vind</span><span class="p">,</span><span class="n">isX</span><span class="o">=</span><span class="bp">True</span><span class="p">,</span><span class="n">sign</span><span class="o">=</span><span class="n">sign</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">dv</span><span class="p">)),</span>
                          <span class="n">steppify</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">spectrum</span><span class="o">+</span><span class="bp">self</span><span class="o">.</span><span class="n">offset</span><span class="o">-</span><span class="bp">self</span><span class="o">.</span><span class="n">errspec</span><span class="o">*</span><span class="bp">self</span><span class="o">.</span><span class="n">scale</span><span class="p">),</span>
                          <span class="n">steppify</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">spectrum</span><span class="o">+</span><span class="bp">self</span><span class="o">.</span><span class="n">offset</span><span class="o">+</span><span class="bp">self</span><span class="o">.</span><span class="n">errspec</span><span class="o">*</span><span class="bp">self</span><span class="o">.</span><span class="n">scale</span><span class="p">),</span>
                          <span class="n">facecolor</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">plotcolor</span><span class="p">,</span> <span class="n">alpha</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">erralpha</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)]</span>
              <span class="k">elif</span> <span class="n">errstyle</span> <span class="o">==</span> <span class="s">&#39;bars&#39;</span><span class="p">:</span>
                  <span class="bp">self</span><span class="o">.</span><span class="n">errorplot</span> <span class="o">=</span> <span class="n">axis</span><span class="o">.</span><span class="n">errorbar</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">vind</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">spectrum</span><span class="o">+</span><span class="bp">self</span><span class="o">.</span><span class="n">offset</span><span class="p">,</span>
                          <span class="n">yerr</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">errspec</span><span class="o">*</span><span class="bp">self</span><span class="o">.</span><span class="n">scale</span><span class="p">,</span> <span class="n">ecolor</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">plotcolor</span><span class="p">,</span> <span class="n">fmt</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span>
                          <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
  
      <span class="k">if</span> <span class="n">vmin</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">None</span><span class="p">:</span> <span class="n">xlo</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">vmin</span>
      <span class="k">else</span><span class="p">:</span> <span class="n">xlo</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">vind</span><span class="o">.</span><span class="n">min</span><span class="p">()</span>
      <span class="k">if</span> <span class="n">vmax</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">None</span><span class="p">:</span> <span class="n">xhi</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">vmax</span>
      <span class="k">else</span><span class="p">:</span> <span class="n">xhi</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">vind</span><span class="o">.</span><span class="n">max</span><span class="p">()</span>
      <span class="n">axis</span><span class="o">.</span><span class="n">set_xlim</span><span class="p">(</span><span class="n">xlo</span><span class="p">,</span><span class="n">xhi</span><span class="p">)</span>
  
      <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">title</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">None</span><span class="p">:</span>
          <span class="n">axis</span><span class="o">.</span><span class="n">set_title</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">title</span><span class="p">)</span>
      <span class="k">elif</span> <span class="bp">self</span><span class="o">.</span><span class="n">xtora</span> <span class="ow">and</span> <span class="bp">self</span><span class="o">.</span><span class="n">ytodec</span><span class="p">:</span>
          <span class="n">axis</span><span class="o">.</span><span class="n">set_title</span><span class="p">(</span><span class="s">&quot;Spectrum at </span><span class="si">%s</span><span class="s"> </span><span class="si">%s</span><span class="s">&quot;</span> <span class="o">%</span>
                  <span class="p">(</span><span class="n">ratos</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">xtora</span><span class="p">(</span><span class="n">i</span><span class="p">)),</span><span class="n">dectos</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">ytodec</span><span class="p">(</span><span class="n">j</span><span class="p">))))</span>
      <span class="k">elif</span> <span class="bp">self</span><span class="o">.</span><span class="n">specname</span><span class="p">:</span>
          <span class="n">axis</span><span class="o">.</span><span class="n">set_title</span><span class="p">(</span><span class="s">&quot;Spectrum of </span><span class="si">%s</span><span class="s">&quot;</span> <span class="o">%</span> <span class="bp">self</span><span class="o">.</span><span class="n">specname</span><span class="p">)</span>
      <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">xunits</span><span class="p">,</span><span class="nb">str</span><span class="p">):</span>
          <span class="n">axis</span><span class="o">.</span><span class="n">set_xlabel</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">xunits</span><span class="p">)</span>
      <span class="k">else</span><span class="p">:</span>
          <span class="n">axis</span><span class="o">.</span><span class="n">set_xlabel</span><span class="p">(</span><span class="s">&quot;V$_{LSR}$ (km s$^{-1}$)&quot;</span><span class="p">)</span>
          <span class="bp">self</span><span class="o">.</span><span class="n">xunits</span> <span class="o">=</span> <span class="s">&#39;km/s&#39;</span>
      <span class="k">if</span> <span class="n">units</span> <span class="ow">in</span> <span class="p">[</span><span class="s">&#39;Ta*&#39;</span><span class="p">,</span><span class="s">&#39;Tastar&#39;</span><span class="p">,</span><span class="s">&#39;K&#39;</span><span class="p">]:</span>
        <span class="n">axis</span><span class="o">.</span><span class="n">set_ylabel</span><span class="p">(</span><span class="s">&quot;$T_A^*$ (K)&quot;</span><span class="p">)</span>
      <span class="k">elif</span> <span class="n">units</span> <span class="o">==</span> <span class="s">&#39;mJy&#39;</span><span class="p">:</span>
        <span class="n">axis</span><span class="o">.</span><span class="n">set_ylabel</span><span class="p">(</span><span class="s">&quot;$S_</span><span class="se">\\</span><span class="s">nu$ (mJy)&quot;</span><span class="p">)</span>
      <span class="k">elif</span> <span class="n">units</span> <span class="o">==</span> <span class="s">&#39;Jy&#39;</span><span class="p">:</span>
        <span class="n">axis</span><span class="o">.</span><span class="n">set_ylabel</span><span class="p">(</span><span class="s">&quot;$S_</span><span class="se">\\</span><span class="s">nu$ (Jy)&quot;</span><span class="p">)</span>
      <span class="k">else</span><span class="p">:</span>
        <span class="n">axis</span><span class="o">.</span><span class="n">set_ylabel</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">units</span><span class="p">)</span>
      <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">autorefresh</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">refresh</span><span class="p">()</span>
  </div>
<div class="viewcode-block" id="SpecPlotter.save"><a class="viewcode-back" href="../../agpy.html#agpy.showspec.SpecPlotter.save">[docs]</a>    <span class="k">def</span> <span class="nf">save</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span><span class="n">fname</span><span class="p">,</span><span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
      <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">      Save the current spectrum (useful for saving baselined data)</span>
<span class="sd">      &quot;&quot;&quot;</span>
      <span class="n">newfile</span> <span class="o">=</span> <span class="n">pyfits</span><span class="o">.</span><span class="n">PrimaryHDU</span><span class="p">(</span><span class="n">data</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">cube</span><span class="p">,</span><span class="n">header</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">header</span><span class="p">)</span>
      <span class="n">newfile</span><span class="o">.</span><span class="n">writeto</span><span class="p">(</span><span class="n">fname</span><span class="p">,</span><span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
</div>
<div class="viewcode-block" id="SpecPlotter.savefig"><a class="viewcode-back" href="../../agpy.html#agpy.showspec.SpecPlotter.savefig">[docs]</a>    <span class="k">def</span> <span class="nf">savefig</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span><span class="n">fname</span><span class="p">,</span><span class="n">bbox_inches</span><span class="o">=</span><span class="s">&#39;tight&#39;</span><span class="p">,</span><span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        simple wrapper of maplotlib&#39;s savefig.  </span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">axis</span><span class="o">.</span><span class="n">figure</span><span class="o">.</span><span class="n">savefig</span><span class="p">(</span><span class="n">fname</span><span class="p">,</span><span class="n">bbox_inches</span><span class="o">=</span><span class="n">bbox_inches</span><span class="p">,</span><span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
  </div>
<div class="viewcode-block" id="SpecPlotter.showlines"><a class="viewcode-back" href="../../agpy.html#agpy.showspec.SpecPlotter.showlines">[docs]</a>    <span class="k">def</span> <span class="nf">showlines</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span><span class="n">linefreqs</span><span class="p">,</span><span class="n">linenames</span><span class="p">,</span><span class="n">ctype</span><span class="o">=</span><span class="s">&#39;freq&#39;</span><span class="p">,</span><span class="n">cunit</span><span class="o">=</span><span class="s">&#39;hz&#39;</span><span class="p">,</span><span class="n">yscale</span><span class="o">=</span><span class="mf">0.8</span><span class="p">,</span><span class="n">vofflines</span><span class="o">=</span><span class="mf">0.0</span><span class="p">,</span>
            <span class="n">voffunit</span><span class="o">=</span><span class="s">&#39;km/s&#39;</span><span class="p">,</span><span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Overplot vertical lines and labels at the frequencies (or velocities) of each line</span>
<span class="sd">  </span>
<span class="sd">        yscale - fraction of maximum at which to label</span>
<span class="sd">        &quot;&quot;&quot;</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">clearlines</span><span class="p">()</span> 

        <span class="k">if</span> <span class="n">ctype</span> <span class="o">!=</span> <span class="s">&#39;freq&#39;</span><span class="p">:</span>
            <span class="k">print</span> <span class="s">&quot;Sorry, non-frequency units not implemented yet.&quot;</span>
            <span class="k">return</span>

        <span class="n">speedoflight</span><span class="o">=</span><span class="mf">2.99792458e5</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">reffreq</span> <span class="ow">and</span> <span class="bp">self</span><span class="o">.</span><span class="n">xunits</span> <span class="ow">in</span> <span class="p">(</span><span class="s">&#39;km/s&#39;</span><span class="p">,</span><span class="s">&#39;m/s&#39;</span><span class="p">):</span>
            <span class="n">linefreqs</span> <span class="o">=</span> <span class="o">-</span><span class="p">(</span><span class="n">array</span><span class="p">(</span><span class="n">linefreqs</span><span class="p">)</span><span class="o">-</span><span class="bp">self</span><span class="o">.</span><span class="n">reffreq</span><span class="p">)</span><span class="o">/</span><span class="bp">self</span><span class="o">.</span><span class="n">reffreq</span> <span class="o">*</span> <span class="n">speedoflight</span>
  
        <span class="k">if</span> <span class="s">&#39;hz&#39;</span> <span class="ow">in</span> <span class="n">cunit</span> <span class="ow">or</span> <span class="s">&#39;Hz&#39;</span> <span class="ow">in</span> <span class="n">cunit</span><span class="p">:</span>
            <span class="n">linefreqs</span> <span class="o">*=</span> <span class="p">(</span><span class="mf">1.0</span> <span class="o">+</span> <span class="n">vofflines</span> <span class="o">/</span> <span class="n">speedoflight</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">linefreqs</span> <span class="o">+=</span> <span class="n">vofflines</span>
      
        <span class="n">ymax</span> <span class="o">=</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">spectrum</span><span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">spectrum</span><span class="o">==</span><span class="bp">self</span><span class="o">.</span><span class="n">spectrum</span><span class="p">])</span><span class="o">.</span><span class="n">max</span><span class="p">()</span> 
        <span class="k">for</span> <span class="n">lf</span><span class="p">,</span><span class="n">ln</span> <span class="ow">in</span> <span class="nb">zip</span><span class="p">(</span><span class="n">linefreqs</span><span class="p">,</span><span class="n">linenames</span><span class="p">):</span>
            <span class="k">if</span> <span class="n">lf</span> <span class="o">&lt;</span> <span class="bp">self</span><span class="o">.</span><span class="n">vind</span><span class="o">.</span><span class="n">max</span><span class="p">()</span> <span class="ow">and</span> <span class="n">lf</span> <span class="o">&gt;</span> <span class="bp">self</span><span class="o">.</span><span class="n">vind</span><span class="o">.</span><span class="n">min</span><span class="p">():</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">linecollections</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">vlines</span><span class="p">(</span><span class="n">lf</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="n">ymax</span><span class="p">,</span><span class="o">**</span><span class="n">kwargs</span><span class="p">))</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">texts</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">text</span><span class="p">(</span><span class="n">lf</span><span class="p">,</span><span class="n">ymax</span><span class="o">*</span><span class="n">yscale</span><span class="p">,</span><span class="n">ln</span><span class="p">,</span><span class="n">rotation</span><span class="o">=</span><span class="s">&#39;vertical&#39;</span><span class="p">,</span><span class="o">**</span><span class="n">kwargs</span><span class="p">))</span>

        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">autorefresh</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">refresh</span><span class="p">()</span>
  </div>
    <span class="k">def</span> <span class="nf">clearlines</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">texts</span><span class="p">)</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">:</span>
            <span class="k">for</span> <span class="n">T</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">texts</span><span class="p">:</span>
                <span class="k">if</span> <span class="n">T</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">axis</span><span class="o">.</span><span class="n">texts</span><span class="p">:</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">axis</span><span class="o">.</span><span class="n">texts</span><span class="o">.</span><span class="n">remove</span><span class="p">(</span><span class="n">T</span><span class="p">)</span>
        <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">linecollections</span><span class="p">)</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">:</span>
            <span class="k">for</span> <span class="n">LC</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">linecollections</span><span class="p">:</span>
                <span class="k">if</span> <span class="n">LC</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">axis</span><span class="o">.</span><span class="n">collections</span><span class="p">:</span> 
                    <span class="bp">self</span><span class="o">.</span><span class="n">axis</span><span class="o">.</span><span class="n">collections</span><span class="o">.</span><span class="n">remove</span><span class="p">(</span><span class="n">LC</span><span class="p">)</span>
  
    <span class="k">def</span> <span class="nf">refresh</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">axis</span><span class="o">.</span><span class="n">figure</span><span class="o">.</span><span class="n">canvas</span><span class="o">.</span><span class="n">draw</span><span class="p">()</span>
</div>
<span class="k">class</span> <span class="nc">FFT</span><span class="p">:</span>
    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span><span class="n">specplotter</span><span class="p">,</span><span class="n">fignum</span><span class="o">=</span><span class="mi">3</span><span class="p">,</span><span class="n">axis</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="n">color</span><span class="o">=</span><span class="s">&#39;k&#39;</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">specplotter</span><span class="o">=</span><span class="n">specplotter</span>
        <span class="k">if</span> <span class="n">axis</span> <span class="ow">is</span> <span class="bp">None</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">fignum</span><span class="o">=</span><span class="n">fignum</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">figure</span><span class="o">=</span><span class="n">figure</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">fignum</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">axis</span><span class="o">=</span><span class="n">gca</span><span class="p">()</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">axis</span><span class="o">=</span><span class="n">axis</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">figure</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">axis</span><span class="o">.</span><span class="n">figure</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">fignum</span><span class="o">=</span><span class="bp">None</span>
        <span class="c">#self.axis.clear()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">color</span><span class="o">=</span><span class="n">color</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">fftplot</span><span class="o">=</span><span class="bp">None</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">setspec</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">setshift</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">clear</span><span class="p">()</span>

    <span class="k">def</span> <span class="nf">__call__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span><span class="n">psd</span><span class="o">=</span><span class="bp">False</span><span class="p">,</span><span class="n">shift</span><span class="o">=</span><span class="bp">True</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">setspec</span><span class="p">()</span>
        <span class="k">if</span> <span class="n">psd</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">psd</span><span class="p">(</span><span class="n">shift</span><span class="o">=</span><span class="n">shift</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">fft</span><span class="p">(</span><span class="n">shift</span><span class="o">=</span><span class="n">shift</span><span class="p">)</span>
    
    <span class="k">def</span> <span class="nf">fft</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span><span class="n">shift</span><span class="o">=</span><span class="bp">True</span><span class="p">,</span><span class="n">logplot</span><span class="o">=</span><span class="bp">False</span><span class="p">,</span><span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">clear</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">setshift</span><span class="p">(</span><span class="n">shift</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">logplot</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">axis</span><span class="o">.</span><span class="n">set_yscale</span><span class="p">(</span><span class="s">&#39;log&#39;</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">axis</span><span class="o">.</span><span class="n">set_yscale</span><span class="p">(</span><span class="s">&#39;linear&#39;</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">fftspec</span> <span class="o">=</span> <span class="n">fft</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">spectofft</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">realfft</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">fftspec</span><span class="o">.</span><span class="n">real</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">imagfft</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">fftspec</span><span class="o">.</span><span class="n">imag</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">fftplot</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">axis</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">shiftfunc</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">realfft</span><span class="p">),</span>
                <span class="n">drawstyle</span><span class="o">=</span><span class="s">&#39;steps-mid&#39;</span><span class="p">,</span><span class="n">color</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">color</span><span class="p">,</span><span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">refresh</span><span class="p">()</span>

    <span class="k">def</span> <span class="nf">psd</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span><span class="n">logplot</span><span class="o">=</span><span class="bp">True</span><span class="p">,</span><span class="n">shift</span><span class="o">=</span><span class="bp">True</span><span class="p">,</span><span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">clear</span><span class="p">()</span>
        <span class="k">if</span> <span class="n">logplot</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">axis</span><span class="o">.</span><span class="n">set_yscale</span><span class="p">(</span><span class="s">&#39;log&#39;</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">axis</span><span class="o">.</span><span class="n">set_yscale</span><span class="p">(</span><span class="s">&#39;linear&#39;</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">setshift</span><span class="p">(</span><span class="n">shift</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">psdspec</span> <span class="o">=</span> <span class="n">fft</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">spectofft</span><span class="p">)</span> <span class="o">*</span> <span class="n">fft</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">spectofft</span><span class="p">[::</span><span class="o">-</span><span class="mi">1</span><span class="p">])</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">psdreal</span> <span class="o">=</span> <span class="nb">abs</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">psdspec</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">fftplot</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">axis</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">shiftfunc</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">psdreal</span><span class="p">),</span>
                <span class="n">drawstyle</span><span class="o">=</span><span class="s">&#39;steps-mid&#39;</span><span class="p">,</span><span class="n">color</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">color</span><span class="p">,</span><span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">logplot</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">axis</span><span class="o">.</span><span class="n">set_yscale</span><span class="p">(</span><span class="s">&#39;log&#39;</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">axis</span><span class="o">.</span><span class="n">set_yscale</span><span class="p">(</span><span class="s">&#39;linear&#39;</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">refresh</span><span class="p">()</span>

    <span class="k">def</span> <span class="nf">setshift</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span><span class="n">shift</span><span class="o">=</span><span class="bp">True</span><span class="p">):</span>
        <span class="k">if</span> <span class="n">shift</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">shiftfunc</span> <span class="o">=</span> <span class="n">fftshift</span>
        <span class="k">else</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">shiftfunc</span> <span class="o">=</span> <span class="k">lambda</span> <span class="n">x</span><span class="p">:</span> <span class="n">x</span>

    <span class="k">def</span> <span class="nf">setspec</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">spectofft</span> <span class="o">=</span> <span class="n">copy</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">specplotter</span><span class="o">.</span><span class="n">spectrum</span><span class="p">)</span>
        <span class="n">OKmask</span> <span class="o">=</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">spectofft</span><span class="o">==</span><span class="bp">self</span><span class="o">.</span><span class="n">spectofft</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">spectofft</span><span class="p">[(</span><span class="bp">True</span><span class="o">-</span><span class="n">OKmask</span><span class="p">)]</span> <span class="o">=</span> <span class="mi">0</span>

    <span class="k">def</span> <span class="nf">clear</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">fftplot</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">None</span><span class="p">:</span>
            <span class="k">for</span> <span class="n">p</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">fftplot</span><span class="p">:</span>
                <span class="n">p</span><span class="o">.</span><span class="n">set_visible</span><span class="p">(</span><span class="bp">False</span><span class="p">)</span>
                <span class="k">if</span> <span class="n">p</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">axis</span><span class="o">.</span><span class="n">lines</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">axis</span><span class="o">.</span><span class="n">lines</span><span class="o">.</span><span class="n">remove</span><span class="p">(</span><span class="n">p</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">axis</span><span class="o">.</span><span class="n">clear</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">refresh</span><span class="p">()</span>

    <span class="k">def</span> <span class="nf">refresh</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">axis</span><span class="o">.</span><span class="n">figure</span><span class="o">.</span><span class="n">canvas</span><span class="o">.</span><span class="n">draw</span><span class="p">()</span>

<span class="k">class</span> <span class="nc">PSD</span><span class="p">(</span><span class="n">FFT</span><span class="p">):</span>
    <span class="k">def</span> <span class="nf">__call__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span><span class="n">shift</span><span class="o">=</span><span class="bp">True</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">setspec</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">setshift</span><span class="p">(</span><span class="n">shift</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">clear</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">psd</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">refresh</span><span class="p">()</span>

<span class="k">class</span> <span class="nc">Baseline</span><span class="p">:</span>
    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span><span class="n">specplotter</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">baselinepars</span>  <span class="o">=</span> <span class="bp">None</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">order</span> <span class="o">=</span> <span class="bp">None</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">basespec</span> <span class="o">=</span> <span class="n">zeros</span><span class="p">(</span><span class="n">specplotter</span><span class="o">.</span><span class="n">spectrum</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">excludemask</span> <span class="o">=</span> <span class="n">zeros</span><span class="p">(</span><span class="n">specplotter</span><span class="o">.</span><span class="n">spectrum</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span><span class="n">dtype</span><span class="o">=</span><span class="s">&#39;bool&#39;</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">OKmask</span> <span class="o">=</span> <span class="n">ones</span><span class="p">(</span><span class="n">specplotter</span><span class="o">.</span><span class="n">spectrum</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span><span class="n">dtype</span><span class="o">=</span><span class="s">&#39;bool&#39;</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">specplotter</span> <span class="o">=</span> <span class="n">specplotter</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">blleg</span> <span class="o">=</span> <span class="bp">None</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">click</span> <span class="o">=</span> <span class="mi">0</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">nclicks_b1</span> <span class="o">=</span> <span class="mi">0</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">nclicks_b2</span> <span class="o">=</span> <span class="mi">0</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">fitregion</span><span class="o">=</span><span class="p">[]</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">excludevelo</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">excludepix</span>  <span class="o">=</span> <span class="p">[]</span>

    <span class="k">def</span> <span class="nf">__call__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">order</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="n">annotate</span><span class="o">=</span><span class="bp">False</span><span class="p">,</span> <span class="n">excludefit</span><span class="o">=</span><span class="bp">False</span><span class="p">,</span> <span class="n">save</span><span class="o">=</span><span class="bp">True</span><span class="p">,</span>
            <span class="n">exclude</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="n">exclusionlevel</span><span class="o">=</span><span class="mf">0.01</span><span class="p">,</span>
            <span class="n">interactive</span><span class="o">=</span><span class="bp">False</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Fit and remove a polynomial from the spectrum.  </span>
<span class="sd">        It will be saved in the variable &quot;self.basespec&quot;</span>
<span class="sd">        and the fit parameters will be saved in &quot;self.order&quot;</span>

<span class="sd">        function baseline(spectrum,xarr=None,xmin=None,xmax=None,order=1,quiet=True,exclude=None):</span>
<span class="sd">        Subtract a baseline from a spectrum</span>
<span class="sd">        If xmin,xmax are not specified, defaults to ignoring first and last 10% of spectrum</span>

<span class="sd">        exclude is a set of start/end indices to ignore when baseline fitting</span>
<span class="sd">        (ignored by setting error to infinite in fitting procedure)</span>

<span class="sd">        excludefit creates a mask based on the fitted gaussian model (assuming</span>
<span class="sd">        that it has a zero-height) using an exclusion level of (exclusionlevel)</span>
<span class="sd">        * the smallest gaussian peak that was fit</span>

<span class="sd">        &quot;basespec&quot; is added back to the spectrum before fitting so you can run this</span>
<span class="sd">        procedure multiple times without losing information</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">specfit</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">specplotter</span><span class="o">.</span><span class="n">specfit</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">order</span> <span class="o">=</span> <span class="n">order</span>
        <span class="n">fitp</span> <span class="o">=</span> <span class="n">zeros</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">order</span><span class="o">+</span><span class="mi">1</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">spectofit</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">specplotter</span><span class="o">.</span><span class="n">spectrum</span><span class="o">+</span><span class="bp">self</span><span class="o">.</span><span class="n">basespec</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">OKmask</span> <span class="o">=</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">spectofit</span><span class="o">==</span><span class="bp">self</span><span class="o">.</span><span class="n">spectofit</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">exclude</span> <span class="o">==</span> <span class="s">&#39;interactive&#39;</span> <span class="ow">or</span> <span class="n">interactive</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">excludemask</span><span class="p">[:]</span> <span class="o">=</span> <span class="bp">True</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">excludevelo</span> <span class="o">=</span> <span class="p">[]</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">excludepix</span>  <span class="o">=</span> <span class="p">[]</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">click</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">specplotter</span><span class="o">.</span><span class="n">axis</span><span class="o">.</span><span class="n">figure</span><span class="o">.</span><span class="n">canvas</span><span class="o">.</span><span class="n">mpl_connect</span><span class="p">(</span><span class="s">&#39;button_press_event&#39;</span><span class="p">,</span><span class="bp">self</span><span class="o">.</span><span class="n">selectregion</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">if</span> <span class="n">excludefit</span> <span class="ow">and</span> <span class="n">specfit</span><span class="o">.</span><span class="n">modelpars</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">None</span><span class="p">:</span>
                <span class="c">#vlo = self.specplotter.specfit.modelpars[1] - 2*self.specplotter.specfit.modelpars[2]</span>
                <span class="c">#vhi = self.specplotter.specfit.modelpars[1] + 2*self.specplotter.specfit.modelpars[2]</span>
                <span class="c">#exclude = [argmin(abs(self.specplotter.vind-vlo)),argmin(abs(self.specplotter.vind-vhi))]</span>
                <span class="n">specfit</span><span class="o">.</span><span class="n">fullsizemodel</span><span class="p">()</span> <span class="c"># make sure the spectrum is the right size</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">excludemask</span> <span class="o">=</span> <span class="nb">abs</span><span class="p">(</span><span class="n">specfit</span><span class="o">.</span><span class="n">model</span><span class="p">)</span> <span class="o">&gt;</span> <span class="n">exclusionlevel</span><span class="o">*</span><span class="nb">abs</span><span class="p">(</span><span class="nb">min</span><span class="p">(</span><span class="n">specfit</span><span class="o">.</span><span class="n">modelpars</span><span class="p">[</span><span class="mi">0</span><span class="p">::</span><span class="mi">3</span><span class="p">]))</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">excludemask</span><span class="p">[:]</span> <span class="o">=</span> <span class="bp">False</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">dofit</span><span class="p">(</span><span class="n">exclude</span><span class="o">=</span><span class="n">exclude</span><span class="p">,</span><span class="n">annotate</span><span class="o">=</span><span class="n">annotate</span><span class="p">,</span><span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">save</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">savefit</span><span class="p">()</span>

    <span class="k">def</span> <span class="nf">dofit</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">exclude</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="n">excludeunits</span><span class="o">=</span><span class="s">&#39;velo&#39;</span><span class="p">,</span> <span class="n">annotate</span><span class="o">=</span><span class="bp">False</span><span class="p">,</span>
            <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Do the baseline fitting and save and plot the results.</span>

<span class="sd">        Can specify a region to exclude using velocity units or pixel units</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="n">exclude</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">None</span> <span class="ow">and</span> <span class="n">excludeunits</span> <span class="ow">in</span> <span class="p">[</span><span class="s">&#39;velo&#39;</span><span class="p">,</span><span class="s">&#39;km/s&#39;</span><span class="p">]:</span>
            <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">exclude</span><span class="p">)</span> <span class="o">%</span> <span class="mi">2</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">excludevelo</span> <span class="o">=</span> <span class="n">exclude</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">excludepix</span> <span class="o">=</span> <span class="p">[]</span>
                <span class="k">for</span> <span class="n">vl</span><span class="p">,</span><span class="n">vu</span> <span class="ow">in</span> <span class="nb">zip</span><span class="p">(</span><span class="n">exclude</span><span class="p">[::</span><span class="mi">2</span><span class="p">],</span><span class="n">exclude</span><span class="p">[</span><span class="mi">1</span><span class="p">::</span><span class="mi">2</span><span class="p">]):</span>
                    <span class="n">xl</span> <span class="o">=</span> <span class="n">argmin</span><span class="p">(</span><span class="nb">abs</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">specplotter</span><span class="o">.</span><span class="n">vind</span><span class="o">-</span><span class="n">vl</span><span class="p">))</span>
                    <span class="n">xu</span> <span class="o">=</span> <span class="n">argmin</span><span class="p">(</span><span class="nb">abs</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">specplotter</span><span class="o">.</span><span class="n">vind</span><span class="o">-</span><span class="n">vu</span><span class="p">))</span>
                    <span class="k">if</span> <span class="n">xl</span> <span class="o">&gt;</span> <span class="n">xu</span><span class="p">:</span> <span class="n">xl</span><span class="p">,</span><span class="n">xu</span><span class="o">=</span><span class="n">xu</span><span class="p">,</span><span class="n">xl</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">excludemask</span><span class="p">[</span><span class="n">xl</span><span class="p">:</span><span class="n">xu</span><span class="p">]</span> <span class="o">=</span> <span class="bp">True</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">excludepix</span> <span class="o">+=</span> <span class="p">[</span><span class="n">xl</span><span class="p">,</span><span class="n">xu</span><span class="p">]</span>
        <span class="k">elif</span> <span class="n">excludeunits</span> <span class="ow">in</span> <span class="p">[</span><span class="s">&#39;pix&#39;</span><span class="p">,</span><span class="s">&#39;pixel&#39;</span><span class="p">,</span><span class="s">&#39;chan&#39;</span><span class="p">,</span><span class="s">&#39;channel&#39;</span><span class="p">]:</span>
            <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">exclude</span><span class="p">)</span> <span class="o">%</span> <span class="mi">2</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">excludepix</span> <span class="o">=</span> <span class="p">[]</span>
                <span class="k">for</span> <span class="n">xl</span><span class="p">,</span><span class="n">xu</span> <span class="ow">in</span> <span class="nb">zip</span><span class="p">(</span><span class="n">exclude</span><span class="p">[::</span><span class="mi">2</span><span class="p">],</span><span class="n">exclude</span><span class="p">[</span><span class="mi">1</span><span class="p">::</span><span class="mi">2</span><span class="p">]):</span>
                    <span class="k">if</span> <span class="n">xl</span> <span class="o">&gt;</span> <span class="n">xu</span><span class="p">:</span> <span class="n">xl</span><span class="p">,</span><span class="n">xu</span><span class="o">=</span><span class="n">xu</span><span class="p">,</span><span class="n">xl</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">excludemask</span><span class="p">[</span><span class="n">xl</span><span class="p">:</span><span class="n">xu</span><span class="p">]</span> <span class="o">=</span> <span class="bp">True</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">excludepix</span> <span class="o">+=</span> <span class="p">[</span><span class="n">xl</span><span class="p">,</span><span class="n">xu</span><span class="p">]</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">specplotter</span><span class="o">.</span><span class="n">spectrum</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">baselinepars</span> <span class="o">=</span> <span class="n">baseline</span><span class="p">(</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">spectofit</span><span class="p">,</span>
                <span class="n">xarr</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">specplotter</span><span class="o">.</span><span class="n">vind</span><span class="p">,</span>
                <span class="n">order</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">order</span><span class="p">,</span> <span class="n">exclude</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> 
                <span class="n">mask</span><span class="o">=</span><span class="p">(</span><span class="bp">True</span><span class="o">-</span><span class="bp">self</span><span class="o">.</span><span class="n">OKmask</span><span class="p">)</span><span class="o">+</span><span class="bp">self</span><span class="o">.</span><span class="n">excludemask</span><span class="p">,</span>
                <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">basespec</span> <span class="o">=</span> <span class="n">poly1d</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">baselinepars</span><span class="p">)(</span><span class="bp">self</span><span class="o">.</span><span class="n">specplotter</span><span class="o">.</span><span class="n">vind</span><span class="p">)</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">specplotter</span><span class="o">.</span><span class="n">spectrumplot</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">None</span><span class="p">:</span> 
            <span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">specplotter</span><span class="o">.</span><span class="n">axis</span><span class="o">.</span><span class="n">lines</span><span class="o">.</span><span class="n">remove</span><span class="p">(</span><span class="n">p</span><span class="p">)</span> <span class="k">for</span> <span class="n">p</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">specplotter</span><span class="o">.</span><span class="n">spectrumplot</span><span class="p">]</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">specplotter</span><span class="o">.</span><span class="n">errorplot</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">None</span><span class="p">:</span> 
            <span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">specplotter</span><span class="o">.</span><span class="n">axis</span><span class="o">.</span><span class="n">collections</span><span class="o">.</span><span class="n">remove</span><span class="p">(</span><span class="n">p</span><span class="p">)</span> <span class="k">for</span> <span class="n">p</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">specplotter</span><span class="o">.</span><span class="n">errorplot</span> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">p</span><span class="p">,</span><span class="n">matplotlib</span><span class="o">.</span><span class="n">collections</span><span class="o">.</span><span class="n">PolyCollection</span><span class="p">)]</span>
            <span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">specplotter</span><span class="o">.</span><span class="n">axis</span><span class="o">.</span><span class="n">lines</span><span class="o">.</span><span class="n">remove</span><span class="p">(</span><span class="n">p</span><span class="p">)</span> <span class="k">for</span> <span class="n">p</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">specplotter</span><span class="o">.</span><span class="n">errorplot</span> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">p</span><span class="p">,</span><span class="n">matplotlib</span><span class="o">.</span><span class="n">lines</span><span class="o">.</span><span class="n">Line2D</span><span class="p">)]</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">specplotter</span><span class="o">.</span><span class="n">plotspec</span><span class="p">(</span><span class="o">**</span><span class="bp">self</span><span class="o">.</span><span class="n">specplotter</span><span class="o">.</span><span class="n">plotkwargs</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">specplotter</span><span class="o">.</span><span class="n">axis</span><span class="o">.</span><span class="n">set_ylim</span><span class="p">(</span>
                <span class="nb">abs</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">specplotter</span><span class="o">.</span><span class="n">spectrum</span><span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">OKmask</span><span class="p">]</span><span class="o">.</span><span class="n">min</span><span class="p">())</span><span class="o">*</span><span class="mf">1.1</span><span class="o">*</span><span class="n">sign</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">specplotter</span><span class="o">.</span><span class="n">spectrum</span><span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">OKmask</span><span class="p">]</span><span class="o">.</span><span class="n">min</span><span class="p">()),</span>
                <span class="nb">abs</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">specplotter</span><span class="o">.</span><span class="n">spectrum</span><span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">OKmask</span><span class="p">]</span><span class="o">.</span><span class="n">max</span><span class="p">())</span><span class="o">*</span><span class="mf">1.1</span><span class="o">*</span><span class="n">sign</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">specplotter</span><span class="o">.</span><span class="n">spectrum</span><span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">OKmask</span><span class="p">]</span><span class="o">.</span><span class="n">max</span><span class="p">()))</span>
        <span class="k">if</span> <span class="n">annotate</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">annotate</span><span class="p">()</span> <span class="c"># refreshes automatically</span>
        <span class="k">elif</span> <span class="bp">self</span><span class="o">.</span><span class="n">specplotter</span><span class="o">.</span><span class="n">autorefresh</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">specplotter</span><span class="o">.</span><span class="n">refresh</span><span class="p">()</span>

    <span class="k">def</span> <span class="nf">selectregion</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span><span class="n">event</span><span class="p">,</span><span class="n">annotate</span><span class="o">=</span><span class="bp">False</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        select regions for baseline fitting</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="n">event</span><span class="o">.</span><span class="n">button</span> <span class="o">==</span> <span class="mi">1</span><span class="p">:</span>
            <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">nclicks_b1</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">bx1</span> <span class="o">=</span> <span class="n">argmin</span><span class="p">(</span><span class="nb">abs</span><span class="p">(</span><span class="n">event</span><span class="o">.</span><span class="n">xdata</span><span class="o">-</span><span class="bp">self</span><span class="o">.</span><span class="n">specplotter</span><span class="o">.</span><span class="n">vind</span><span class="p">))</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">excludevelo</span> <span class="o">+=</span> <span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">specplotter</span><span class="o">.</span><span class="n">vind</span><span class="p">]</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">excludepix</span>  <span class="o">+=</span> <span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">bx1</span><span class="p">]</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">nclicks_b1</span> <span class="o">+=</span> <span class="mi">1</span>
            <span class="k">elif</span> <span class="bp">self</span><span class="o">.</span><span class="n">nclicks_b1</span> <span class="o">==</span> <span class="mi">1</span><span class="p">:</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">bx2</span> <span class="o">=</span> <span class="n">argmin</span><span class="p">(</span><span class="nb">abs</span><span class="p">(</span><span class="n">event</span><span class="o">.</span><span class="n">xdata</span><span class="o">-</span><span class="bp">self</span><span class="o">.</span><span class="n">specplotter</span><span class="o">.</span><span class="n">vind</span><span class="p">))</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">nclicks_b1</span> <span class="o">-=</span> <span class="mi">1</span>
                <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">bx1</span> <span class="o">&gt;</span> <span class="bp">self</span><span class="o">.</span><span class="n">bx2</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">bx1</span><span class="p">,</span><span class="bp">self</span><span class="o">.</span><span class="n">bx2</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">bx2</span><span class="p">,</span><span class="bp">self</span><span class="o">.</span><span class="n">bx1</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">fitregion</span> <span class="o">+=</span> <span class="bp">self</span><span class="o">.</span><span class="n">specplotter</span><span class="o">.</span><span class="n">axis</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span>
                        <span class="bp">self</span><span class="o">.</span><span class="n">specplotter</span><span class="o">.</span><span class="n">vind</span><span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">bx1</span><span class="p">:</span><span class="bp">self</span><span class="o">.</span><span class="n">bx2</span><span class="p">],</span>
                        <span class="bp">self</span><span class="o">.</span><span class="n">specplotter</span><span class="o">.</span><span class="n">spectrum</span><span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">bx1</span><span class="p">:</span><span class="bp">self</span><span class="o">.</span><span class="n">bx2</span><span class="p">]</span><span class="o">+</span><span class="bp">self</span><span class="o">.</span><span class="n">specplotter</span><span class="o">.</span><span class="n">offset</span><span class="p">,</span>
                        <span class="n">drawstyle</span><span class="o">=</span><span class="s">&#39;steps-mid&#39;</span><span class="p">,</span>
                        <span class="n">color</span><span class="o">=</span><span class="s">&#39;g&#39;</span><span class="p">,</span><span class="n">alpha</span><span class="o">=</span><span class="mf">0.5</span><span class="p">)</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">specplotter</span><span class="o">.</span><span class="n">refresh</span><span class="p">()</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">excludemask</span><span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">bx1</span><span class="p">:</span><span class="bp">self</span><span class="o">.</span><span class="n">bx2</span><span class="p">]</span> <span class="o">=</span> <span class="bp">False</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">excludevelo</span> <span class="o">+=</span> <span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">specplotter</span><span class="o">.</span><span class="n">vind</span><span class="p">]</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">excludepix</span>  <span class="o">+=</span> <span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">bx2</span><span class="p">]</span>
        <span class="k">if</span> <span class="n">event</span><span class="o">.</span><span class="n">button</span> <span class="ow">in</span> <span class="p">[</span><span class="mi">2</span><span class="p">,</span><span class="mi">3</span><span class="p">]:</span>
            <span class="n">disconnect</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">click</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">dofit</span><span class="p">(</span><span class="n">exclude</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span><span class="n">annotate</span><span class="o">=</span><span class="n">annotate</span><span class="p">)</span>
            <span class="k">for</span> <span class="n">p</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">fitregion</span><span class="p">:</span>
                <span class="n">p</span><span class="o">.</span><span class="n">set_visible</span><span class="p">(</span><span class="bp">False</span><span class="p">)</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">specplotter</span><span class="o">.</span><span class="n">axis</span><span class="o">.</span><span class="n">lines</span><span class="o">.</span><span class="n">remove</span><span class="p">(</span><span class="n">p</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">fitregion</span><span class="o">=</span><span class="p">[]</span> <span class="c"># I should be able to just remove from the list... but it breaks the loop...</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">specplotter</span><span class="o">.</span><span class="n">refresh</span><span class="p">()</span>

    <span class="k">def</span> <span class="nf">annotate</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span><span class="n">loc</span><span class="o">=</span><span class="s">&#39;upper left&#39;</span><span class="p">):</span>
        <span class="n">bltext</span> <span class="o">=</span> <span class="s">&quot;bl: $y=$&quot;</span><span class="o">+</span><span class="s">&quot;&quot;</span><span class="o">.</span><span class="n">join</span><span class="p">([</span><span class="s">&quot;$</span><span class="si">%+6.3g</span><span class="s">x^{</span><span class="si">%i</span><span class="s">}$&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="n">f</span><span class="p">,</span><span class="bp">self</span><span class="o">.</span><span class="n">order</span><span class="o">-</span><span class="n">i</span><span class="p">)</span>
            <span class="k">for</span> <span class="n">i</span><span class="p">,</span><span class="n">f</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">baselinepars</span><span class="p">)])</span>
        <span class="c">#self.blleg = text(xloc,yloc     ,bltext,transform = self.specplotter.axis.transAxes)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">clearlegend</span><span class="p">()</span>
        <span class="n">pl</span> <span class="o">=</span> <span class="n">matplotlib</span><span class="o">.</span><span class="n">collections</span><span class="o">.</span><span class="n">CircleCollection</span><span class="p">([</span><span class="mi">0</span><span class="p">],</span><span class="n">edgecolors</span><span class="o">=</span><span class="p">[</span><span class="s">&#39;k&#39;</span><span class="p">])</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">blleg</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">specplotter</span><span class="o">.</span><span class="n">axis</span><span class="o">.</span><span class="n">legend</span><span class="p">(</span>
                <span class="p">(</span><span class="n">pl</span><span class="p">,),</span>
                <span class="p">(</span><span class="n">bltext</span><span class="p">,),</span><span class="n">loc</span><span class="o">=</span><span class="n">loc</span><span class="p">,</span><span class="n">markerscale</span><span class="o">=</span><span class="mf">0.001</span><span class="p">,</span>
                <span class="n">borderpad</span><span class="o">=</span><span class="mf">0.1</span><span class="p">,</span> <span class="n">handlelength</span><span class="o">=</span><span class="mf">0.1</span><span class="p">,</span> <span class="n">handletextpad</span><span class="o">=</span><span class="mf">0.1</span>
                <span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">specplotter</span><span class="o">.</span><span class="n">axis</span><span class="o">.</span><span class="n">add_artist</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">blleg</span><span class="p">)</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">specplotter</span><span class="o">.</span><span class="n">autorefresh</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">specplotter</span><span class="o">.</span><span class="n">refresh</span><span class="p">()</span>
  
    <span class="k">def</span> <span class="nf">clearlegend</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">blleg</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">None</span><span class="p">:</span> 
            <span class="bp">self</span><span class="o">.</span><span class="n">blleg</span><span class="o">.</span><span class="n">set_visible</span><span class="p">(</span><span class="bp">False</span><span class="p">)</span>
            <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">blleg</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">specplotter</span><span class="o">.</span><span class="n">axis</span><span class="o">.</span><span class="n">artists</span><span class="p">:</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">specplotter</span><span class="o">.</span><span class="n">axis</span><span class="o">.</span><span class="n">artists</span><span class="o">.</span><span class="n">remove</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">blleg</span><span class="p">)</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">specplotter</span><span class="o">.</span><span class="n">autorefresh</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">specplotter</span><span class="o">.</span><span class="n">refresh</span><span class="p">()</span>

    <span class="k">def</span> <span class="nf">savefit</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">baselinepars</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">None</span><span class="p">:</span>
            <span class="k">for</span> <span class="n">ii</span><span class="p">,</span><span class="n">p</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">baselinepars</span><span class="p">):</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">specplotter</span><span class="o">.</span><span class="n">header</span><span class="o">.</span><span class="n">update</span><span class="p">(</span><span class="s">&#39;BLCOEF</span><span class="si">%0.2i</span><span class="s">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="n">ii</span><span class="p">),</span><span class="n">p</span><span class="p">,</span><span class="n">comment</span><span class="o">=</span><span class="s">&quot;Baseline power-law best-fit coefficient x^</span><span class="si">%i</span><span class="s">&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">order</span><span class="o">-</span><span class="n">ii</span><span class="o">-</span><span class="mi">1</span><span class="p">))</span>


<span class="k">class</span> <span class="nc">Specfit</span><span class="p">:</span>

    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span><span class="n">specplotter</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">model</span> <span class="o">=</span> <span class="bp">None</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">modelpars</span> <span class="o">=</span> <span class="bp">None</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">modelerrs</span> <span class="o">=</span> <span class="bp">None</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">modelplot</span> <span class="o">=</span> <span class="bp">None</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">guessplot</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">fitregion</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">ngauss</span> <span class="o">=</span> <span class="mi">0</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">nclicks_b1</span> <span class="o">=</span> <span class="mi">0</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">nclicks_b2</span> <span class="o">=</span> <span class="mi">0</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">gx1</span> <span class="o">=</span> <span class="mi">0</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">gx2</span> <span class="o">=</span> <span class="n">specplotter</span><span class="o">.</span><span class="n">spectrum</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">guesses</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">click</span> <span class="o">=</span> <span class="mi">0</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">fitkwargs</span> <span class="o">=</span> <span class="p">{}</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">auto</span> <span class="o">=</span> <span class="bp">False</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">autoannotate</span> <span class="o">=</span> <span class="bp">True</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">specplotter</span> <span class="o">=</span> <span class="n">specplotter</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">gaussleg</span><span class="o">=</span><span class="bp">None</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">residuals</span><span class="o">=</span><span class="bp">None</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">setfitspec</span><span class="p">()</span>
        <span class="c">#self.seterrspec()</span>

    <span class="k">def</span> <span class="nf">__call__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">interactive</span><span class="o">=</span><span class="bp">False</span><span class="p">,</span> <span class="n">usemoments</span><span class="o">=</span><span class="bp">True</span><span class="p">,</span> <span class="n">fitcolor</span><span class="o">=</span><span class="s">&#39;r&#39;</span><span class="p">,</span>
            <span class="n">multifit</span><span class="o">=</span><span class="bp">False</span><span class="p">,</span> <span class="n">guesses</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="n">annotate</span><span class="o">=</span><span class="bp">True</span><span class="p">,</span> <span class="n">save</span><span class="o">=</span><span class="bp">True</span><span class="p">,</span>
            <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Fit gaussians to a spectrum</span>

<span class="sd">        guesses = [height,amplitude,center,width]</span>
<span class="sd">        &quot;&quot;&quot;</span>
  
        <span class="bp">self</span><span class="o">.</span><span class="n">fitcolor</span> <span class="o">=</span> <span class="n">fitcolor</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">clear</span><span class="p">()</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">ngauss</span> <span class="o">=</span> <span class="mi">0</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">fitkwargs</span> <span class="o">=</span> <span class="n">kwargs</span>
        <span class="k">if</span> <span class="n">interactive</span><span class="p">:</span>
            <span class="k">print</span> <span class="s">&quot;Left-click twice to select a fitting range, then middle-click twice to select a peak and width&quot;</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">nclicks_b1</span> <span class="o">=</span> <span class="mi">0</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">nclicks_b2</span> <span class="o">=</span> <span class="mi">0</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">guesses</span> <span class="o">=</span> <span class="p">[]</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">click</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">specplotter</span><span class="o">.</span><span class="n">axis</span><span class="o">.</span><span class="n">figure</span><span class="o">.</span><span class="n">canvas</span><span class="o">.</span><span class="n">mpl_connect</span><span class="p">(</span><span class="s">&#39;button_press_event&#39;</span><span class="p">,</span><span class="bp">self</span><span class="o">.</span><span class="n">makeguess</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">autoannotate</span> <span class="o">=</span> <span class="n">annotate</span>
        <span class="k">elif</span> <span class="n">multifit</span><span class="p">:</span>
            <span class="k">if</span> <span class="n">guesses</span> <span class="ow">is</span> <span class="bp">None</span><span class="p">:</span>
                <span class="k">print</span> <span class="s">&quot;You must input guesses when using multifit.  Also, baseline first!&quot;</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">guesses</span> <span class="o">=</span> <span class="n">guesses</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">multifit</span><span class="p">()</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">autoannotate</span> <span class="o">=</span> <span class="n">annotate</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="c">#print &quot;Non-interactive, 1D fit with automatic guessing&quot;</span>
            <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">specplotter</span><span class="o">.</span><span class="n">baseline</span><span class="o">.</span><span class="n">order</span> <span class="ow">is</span> <span class="bp">None</span><span class="p">:</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">specplotter</span><span class="o">.</span><span class="n">baseline</span><span class="o">.</span><span class="n">order</span><span class="o">=</span><span class="mi">0</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">onedfit</span><span class="p">(</span><span class="n">usemoments</span><span class="o">=</span><span class="n">usemoments</span><span class="p">,</span><span class="n">annotate</span><span class="o">=</span><span class="n">annotate</span><span class="p">,</span><span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">onedfit</span><span class="p">(</span><span class="n">usemoments</span><span class="o">=</span><span class="n">usemoments</span><span class="p">,</span><span class="n">annotate</span><span class="o">=</span><span class="n">annotate</span><span class="p">,</span>
                        <span class="n">vheight</span><span class="o">=</span><span class="bp">False</span><span class="p">,</span><span class="n">height</span><span class="o">=</span><span class="mf">0.0</span><span class="p">,</span><span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
            <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">specplotter</span><span class="o">.</span><span class="n">autorefresh</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">specplotter</span><span class="o">.</span><span class="n">refresh</span><span class="p">()</span>
        <span class="k">if</span> <span class="n">save</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">savefit</span><span class="p">()</span>

    <span class="k">def</span> <span class="nf">seterrspec</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span><span class="n">usestd</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span><span class="n">useresiduals</span><span class="o">=</span><span class="bp">True</span><span class="p">):</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">specplotter</span><span class="o">.</span><span class="n">errspec</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">None</span> <span class="ow">and</span> <span class="ow">not</span> <span class="n">usestd</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">errspec</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">specplotter</span><span class="o">.</span><span class="n">errspec</span>
        <span class="k">elif</span> <span class="bp">self</span><span class="o">.</span><span class="n">residuals</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">None</span> <span class="ow">and</span> <span class="n">useresiduals</span><span class="p">:</span> 
            <span class="bp">self</span><span class="o">.</span><span class="n">errspec</span> <span class="o">=</span> <span class="n">ones</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">spectofit</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span> <span class="o">*</span> <span class="bp">self</span><span class="o">.</span><span class="n">residuals</span><span class="o">.</span><span class="n">std</span><span class="p">()</span>
        <span class="k">else</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">errspec</span> <span class="o">=</span> <span class="n">ones</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">spectofit</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span> <span class="o">*</span> <span class="bp">self</span><span class="o">.</span><span class="n">spectofit</span><span class="o">.</span><span class="n">std</span><span class="p">()</span>

    <span class="k">def</span> <span class="nf">setfitspec</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">spectofit</span> <span class="o">=</span> <span class="n">copy</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">specplotter</span><span class="o">.</span><span class="n">spectrum</span><span class="p">)</span>
        <span class="n">OKmask</span> <span class="o">=</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">spectofit</span><span class="o">==</span><span class="bp">self</span><span class="o">.</span><span class="n">spectofit</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">spectofit</span><span class="p">[(</span><span class="bp">True</span><span class="o">-</span><span class="n">OKmask</span><span class="p">)]</span> <span class="o">=</span> <span class="mi">0</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">seterrspec</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">errspec</span><span class="p">[(</span><span class="bp">True</span><span class="o">-</span><span class="n">OKmask</span><span class="p">)]</span> <span class="o">=</span> <span class="mf">1e10</span>

    <span class="k">def</span> <span class="nf">multifit</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">ngauss</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">guesses</span><span class="p">)</span><span class="o">/</span><span class="mi">3</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">setfitspec</span><span class="p">()</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">fitkwargs</span><span class="o">.</span><span class="n">has_key</span><span class="p">(</span><span class="s">&#39;negamp&#39;</span><span class="p">):</span> <span class="bp">self</span><span class="o">.</span><span class="n">fitkwargs</span><span class="o">.</span><span class="n">pop</span><span class="p">(</span><span class="s">&#39;negamp&#39;</span><span class="p">)</span>
        <span class="n">mpp</span><span class="p">,</span><span class="n">model</span><span class="p">,</span><span class="n">mpperr</span><span class="p">,</span><span class="n">chi2</span> <span class="o">=</span> <span class="n">gaussfitter</span><span class="o">.</span><span class="n">multigaussfit</span><span class="p">(</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">specplotter</span><span class="o">.</span><span class="n">vind</span><span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">gx1</span><span class="p">:</span><span class="bp">self</span><span class="o">.</span><span class="n">gx2</span><span class="p">],</span> 
                <span class="bp">self</span><span class="o">.</span><span class="n">spectofit</span><span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">gx1</span><span class="p">:</span><span class="bp">self</span><span class="o">.</span><span class="n">gx2</span><span class="p">],</span> 
                <span class="n">err</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">errspec</span><span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">gx1</span><span class="p">:</span><span class="bp">self</span><span class="o">.</span><span class="n">gx2</span><span class="p">],</span>
                <span class="n">ngauss</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">ngauss</span><span class="p">,</span>
                <span class="n">params</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">guesses</span><span class="p">,</span>
                <span class="o">**</span><span class="bp">self</span><span class="o">.</span><span class="n">fitkwargs</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">chi2</span> <span class="o">=</span> <span class="n">chi2</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">dof</span>  <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">gx2</span><span class="o">-</span><span class="bp">self</span><span class="o">.</span><span class="n">gx1</span><span class="o">-</span><span class="bp">self</span><span class="o">.</span><span class="n">ngauss</span><span class="o">*</span><span class="mi">3</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">model</span> <span class="o">=</span> <span class="n">model</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">modelpars</span> <span class="o">=</span> <span class="n">mpp</span><span class="o">.</span><span class="n">tolist</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">modelerrs</span> <span class="o">=</span> <span class="n">mpperr</span><span class="o">.</span><span class="n">tolist</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">modelplot</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">specplotter</span><span class="o">.</span><span class="n">axis</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">specplotter</span><span class="o">.</span><span class="n">vind</span><span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">gx1</span><span class="p">:</span><span class="bp">self</span><span class="o">.</span><span class="n">gx2</span><span class="p">],</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="o">+</span><span class="bp">self</span><span class="o">.</span><span class="n">specplotter</span><span class="o">.</span><span class="n">offset</span><span class="p">,</span> <span class="n">color</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">fitcolor</span><span class="p">,</span> <span class="n">linewidth</span><span class="o">=</span><span class="mf">0.5</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">residuals</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">spectofit</span><span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">gx1</span><span class="p">:</span><span class="bp">self</span><span class="o">.</span><span class="n">gx2</span><span class="p">]</span> <span class="o">-</span> <span class="bp">self</span><span class="o">.</span><span class="n">model</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">autoannotate</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">annotate</span><span class="p">()</span>
    
    <span class="k">def</span> <span class="nf">onedfit</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">usemoments</span><span class="o">=</span><span class="bp">True</span><span class="p">,</span> <span class="n">annotate</span><span class="o">=</span><span class="bp">True</span><span class="p">,</span> <span class="n">vheight</span><span class="o">=</span><span class="bp">True</span><span class="p">,</span> <span class="n">height</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span> <span class="n">negamp</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span><span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">ngauss</span> <span class="o">=</span> <span class="mi">1</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">auto</span> <span class="o">=</span> <span class="bp">True</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">setfitspec</span><span class="p">()</span>
        <span class="k">if</span> <span class="n">usemoments</span><span class="p">:</span> <span class="c"># this can be done within gaussfit but I want to save them</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">guesses</span> <span class="o">=</span> <span class="n">gaussfitter</span><span class="o">.</span><span class="n">onedmoments</span><span class="p">(</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">specplotter</span><span class="o">.</span><span class="n">vind</span><span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">gx1</span><span class="p">:</span><span class="bp">self</span><span class="o">.</span><span class="n">gx2</span><span class="p">],</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">spectofit</span><span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">gx1</span><span class="p">:</span><span class="bp">self</span><span class="o">.</span><span class="n">gx2</span><span class="p">],</span>
                    <span class="n">vheight</span><span class="o">=</span><span class="n">vheight</span><span class="p">,</span><span class="n">negamp</span><span class="o">=</span><span class="n">negamp</span><span class="p">,</span><span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
            <span class="k">if</span> <span class="n">vheight</span> <span class="ow">is</span> <span class="bp">False</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">guesses</span> <span class="o">=</span> <span class="p">[</span><span class="n">height</span><span class="p">]</span><span class="o">+</span><span class="bp">self</span><span class="o">.</span><span class="n">guesses</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">if</span> <span class="n">negamp</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">guesses</span> <span class="o">=</span> <span class="p">[</span><span class="n">height</span><span class="p">,</span><span class="o">-</span><span class="mi">1</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">1</span><span class="p">]</span>
            <span class="k">else</span><span class="p">:</span>  <span class="bp">self</span><span class="o">.</span><span class="n">guesses</span> <span class="o">=</span> <span class="p">[</span><span class="n">height</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">1</span><span class="p">]</span>
        <span class="n">mpp</span><span class="p">,</span><span class="n">model</span><span class="p">,</span><span class="n">mpperr</span><span class="p">,</span><span class="n">chi2</span> <span class="o">=</span> <span class="n">gaussfitter</span><span class="o">.</span><span class="n">onedgaussfit</span><span class="p">(</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">specplotter</span><span class="o">.</span><span class="n">vind</span><span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">gx1</span><span class="p">:</span><span class="bp">self</span><span class="o">.</span><span class="n">gx2</span><span class="p">],</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">spectofit</span><span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">gx1</span><span class="p">:</span><span class="bp">self</span><span class="o">.</span><span class="n">gx2</span><span class="p">],</span>
                <span class="n">err</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">errspec</span><span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">gx1</span><span class="p">:</span><span class="bp">self</span><span class="o">.</span><span class="n">gx2</span><span class="p">],</span>
                <span class="n">vheight</span><span class="o">=</span><span class="n">vheight</span><span class="p">,</span>
                <span class="n">params</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">guesses</span><span class="p">,</span>
                <span class="o">**</span><span class="bp">self</span><span class="o">.</span><span class="n">fitkwargs</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">chi2</span> <span class="o">=</span> <span class="n">chi2</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">dof</span>  <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">gx2</span><span class="o">-</span><span class="bp">self</span><span class="o">.</span><span class="n">gx1</span><span class="o">-</span><span class="bp">self</span><span class="o">.</span><span class="n">ngauss</span><span class="o">*</span><span class="mi">3</span><span class="o">-</span><span class="n">vheight</span>
        <span class="k">if</span> <span class="n">vheight</span><span class="p">:</span> 
            <span class="bp">self</span><span class="o">.</span><span class="n">specplotter</span><span class="o">.</span><span class="n">baseline</span><span class="o">.</span><span class="n">baselinepars</span> <span class="o">=</span> <span class="n">mpp</span><span class="p">[:</span><span class="mi">1</span><span class="p">]</span> <span class="c"># first item in list form</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">model</span> <span class="o">=</span> <span class="n">model</span> <span class="o">-</span> <span class="n">mpp</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
        <span class="k">else</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">model</span> <span class="o">=</span> <span class="n">model</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">residuals</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">spectofit</span><span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">gx1</span><span class="p">:</span><span class="bp">self</span><span class="o">.</span><span class="n">gx2</span><span class="p">]</span> <span class="o">-</span> <span class="bp">self</span><span class="o">.</span><span class="n">model</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">modelpars</span> <span class="o">=</span> <span class="n">mpp</span><span class="p">[</span><span class="mi">1</span><span class="p">:]</span><span class="o">.</span><span class="n">tolist</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">modelerrs</span> <span class="o">=</span> <span class="n">mpperr</span><span class="p">[</span><span class="mi">1</span><span class="p">:]</span><span class="o">.</span><span class="n">tolist</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">modelplot</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">specplotter</span><span class="o">.</span><span class="n">axis</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">specplotter</span><span class="o">.</span><span class="n">vind</span><span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">gx1</span><span class="p">:</span><span class="bp">self</span><span class="o">.</span><span class="n">gx2</span><span class="p">],</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="o">+</span><span class="bp">self</span><span class="o">.</span><span class="n">specplotter</span><span class="o">.</span><span class="n">offset</span><span class="p">,</span> <span class="n">color</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">fitcolor</span><span class="p">,</span> <span class="n">linewidth</span><span class="o">=</span><span class="mf">0.5</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">annotate</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">annotate</span><span class="p">()</span>
            <span class="k">if</span> <span class="n">vheight</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">specplotter</span><span class="o">.</span><span class="n">baseline</span><span class="o">.</span><span class="n">annotate</span><span class="p">()</span>

    <span class="k">def</span> <span class="nf">fullsizemodel</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        If the gaussian was fit to a sub-region of the spectrum,</span>
<span class="sd">        expand it (with zeros) to fill the spectrum.  You can </span>
<span class="sd">        always recover the original by:</span>
<span class="sd">        origmodel = model[gx1:gx2]</span>
<span class="sd">        &quot;&quot;&quot;</span>

        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">shape</span> <span class="o">!=</span> <span class="bp">self</span><span class="o">.</span><span class="n">specplotter</span><span class="o">.</span><span class="n">spectrum</span><span class="o">.</span><span class="n">shape</span><span class="p">:</span>
            <span class="n">temp</span> <span class="o">=</span> <span class="n">zeros</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">specplotter</span><span class="o">.</span><span class="n">spectrum</span><span class="o">.</span><span class="n">shape</span><span class="p">)</span>
            <span class="n">temp</span><span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">gx1</span><span class="p">:</span><span class="bp">self</span><span class="o">.</span><span class="n">gx2</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">model</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">model</span> <span class="o">=</span> <span class="n">temp</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">residuals</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">spectofit</span> <span class="o">-</span> <span class="bp">self</span><span class="o">.</span><span class="n">model</span>

    <span class="k">def</span> <span class="nf">plotresiduals</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span><span class="n">fig</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span><span class="n">axis</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span><span class="n">clear</span><span class="o">=</span><span class="bp">True</span><span class="p">,</span><span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Plot residuals of the fit.  Specify a figure or</span>
<span class="sd">        axis; defaults to figure(2).</span>

<span class="sd">        kwargs are passed to matplotlib plot</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="n">axis</span> <span class="ow">is</span> <span class="bp">None</span><span class="p">:</span>
            <span class="n">fig</span><span class="o">=</span><span class="n">figure</span><span class="p">(</span><span class="mi">2</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">residualaxis</span> <span class="o">=</span> <span class="n">gca</span><span class="p">()</span>
            <span class="k">if</span> <span class="n">clear</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">residualaxis</span><span class="o">.</span><span class="n">clear</span><span class="p">()</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">residualaxis</span> <span class="o">=</span> <span class="n">axis</span>
            <span class="k">if</span> <span class="n">clear</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">residualaxis</span><span class="o">.</span><span class="n">clear</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">residualplot</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">residualaxis</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">specplotter</span><span class="o">.</span><span class="n">vind</span><span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">gx1</span><span class="p">:</span><span class="bp">self</span><span class="o">.</span><span class="n">gx2</span><span class="p">],</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">residuals</span><span class="p">,</span><span class="n">drawstyle</span><span class="o">=</span><span class="s">&#39;steps-mid&#39;</span><span class="p">,</span>
                <span class="n">linewidth</span><span class="o">=</span><span class="mf">0.5</span><span class="p">,</span> <span class="n">color</span><span class="o">=</span><span class="s">&#39;k&#39;</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">specplotter</span><span class="o">.</span><span class="n">vmin</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">None</span> <span class="ow">and</span> <span class="bp">self</span><span class="o">.</span><span class="n">specplotter</span><span class="o">.</span><span class="n">vmax</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">None</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">residualaxis</span><span class="o">.</span><span class="n">set_xlim</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">specplotter</span><span class="o">.</span><span class="n">vmin</span><span class="p">,</span><span class="bp">self</span><span class="o">.</span><span class="n">specplotter</span><span class="o">.</span><span class="n">vmax</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">residualaxis</span><span class="o">.</span><span class="n">figure</span><span class="o">.</span><span class="n">canvas</span><span class="o">.</span><span class="n">draw</span><span class="p">()</span>

    <span class="k">def</span> <span class="nf">annotate</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span><span class="n">loc</span><span class="o">=</span><span class="s">&#39;upper right&#39;</span><span class="p">):</span>
        <span class="c">#text(xloc,yloc     ,&quot;c=%g&quot; % self.modelpars[1],transform = self.specplotter.axis.transAxes)</span>
        <span class="c">#text(xloc,yloc-0.05,&quot;w=%g&quot; % self.modelpars[2],transform = self.specplotter.axis.transAxes)</span>
        <span class="c">#text(xloc,yloc-0.10,&quot;a=%g&quot; % self.modelpars[0],transform = self.specplotter.axis.transAxes)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">clearlegend</span><span class="p">()</span>
        <span class="n">pl</span> <span class="o">=</span> <span class="n">matplotlib</span><span class="o">.</span><span class="n">collections</span><span class="o">.</span><span class="n">CircleCollection</span><span class="p">([</span><span class="mi">0</span><span class="p">],</span><span class="n">edgecolors</span><span class="o">=</span><span class="p">[</span><span class="s">&#39;k&#39;</span><span class="p">])</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">gaussleg</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">specplotter</span><span class="o">.</span><span class="n">axis</span><span class="o">.</span><span class="n">legend</span><span class="p">(</span>
                <span class="nb">tuple</span><span class="p">([</span><span class="n">pl</span><span class="p">]</span><span class="o">*</span><span class="mi">3</span><span class="o">*</span><span class="bp">self</span><span class="o">.</span><span class="n">ngauss</span><span class="p">),</span>
                <span class="nb">tuple</span><span class="p">(</span><span class="n">flatten</span><span class="p">(</span>
                    <span class="p">[(</span><span class="s">&quot;c</span><span class="si">%i</span><span class="s">=</span><span class="si">%6.4g</span><span class="s"> $</span><span class="se">\\</span><span class="s">pm$ </span><span class="si">%6.4g</span><span class="s">&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="n">jj</span><span class="p">,</span><span class="bp">self</span><span class="o">.</span><span class="n">modelpars</span><span class="p">[</span><span class="mi">1</span><span class="o">+</span><span class="n">jj</span><span class="o">*</span><span class="mi">3</span><span class="p">],</span><span class="bp">self</span><span class="o">.</span><span class="n">modelerrs</span><span class="p">[</span><span class="mi">1</span><span class="o">+</span><span class="n">jj</span><span class="o">*</span><span class="mi">3</span><span class="p">]),</span>
                      <span class="s">&quot;w</span><span class="si">%i</span><span class="s">=</span><span class="si">%6.4g</span><span class="s"> $</span><span class="se">\\</span><span class="s">pm$ </span><span class="si">%6.4g</span><span class="s">&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="n">jj</span><span class="p">,</span><span class="bp">self</span><span class="o">.</span><span class="n">modelpars</span><span class="p">[</span><span class="mi">2</span><span class="o">+</span><span class="n">jj</span><span class="o">*</span><span class="mi">3</span><span class="p">],</span><span class="bp">self</span><span class="o">.</span><span class="n">modelerrs</span><span class="p">[</span><span class="mi">2</span><span class="o">+</span><span class="n">jj</span><span class="o">*</span><span class="mi">3</span><span class="p">]),</span>
                      <span class="s">&quot;a</span><span class="si">%i</span><span class="s">=</span><span class="si">%6.4g</span><span class="s"> $</span><span class="se">\\</span><span class="s">pm$ </span><span class="si">%6.4g</span><span class="s">&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="n">jj</span><span class="p">,</span><span class="bp">self</span><span class="o">.</span><span class="n">modelpars</span><span class="p">[</span><span class="mi">0</span><span class="o">+</span><span class="n">jj</span><span class="o">*</span><span class="mi">3</span><span class="p">],</span><span class="bp">self</span><span class="o">.</span><span class="n">modelerrs</span><span class="p">[</span><span class="mi">0</span><span class="o">+</span><span class="n">jj</span><span class="o">*</span><span class="mi">3</span><span class="p">]))</span>
                      <span class="k">for</span> <span class="n">jj</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">ngauss</span><span class="p">)])),</span>
                <span class="n">loc</span><span class="o">=</span><span class="n">loc</span><span class="p">,</span><span class="n">markerscale</span><span class="o">=</span><span class="mf">0.01</span><span class="p">,</span>
                <span class="n">borderpad</span><span class="o">=</span><span class="mf">0.1</span><span class="p">,</span> <span class="n">handlelength</span><span class="o">=</span><span class="mf">0.1</span><span class="p">,</span> <span class="n">handletextpad</span><span class="o">=</span><span class="mf">0.1</span>
                <span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">gaussleg</span><span class="o">.</span><span class="n">draggable</span><span class="p">(</span><span class="bp">True</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">specplotter</span><span class="o">.</span><span class="n">axis</span><span class="o">.</span><span class="n">add_artist</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">gaussleg</span><span class="p">)</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">specplotter</span><span class="o">.</span><span class="n">autorefresh</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">specplotter</span><span class="o">.</span><span class="n">refresh</span><span class="p">()</span>

    <span class="k">def</span> <span class="nf">selectregion</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span><span class="n">event</span><span class="p">):</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">nclicks_b1</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">gx1</span> <span class="o">=</span> <span class="n">argmin</span><span class="p">(</span><span class="nb">abs</span><span class="p">(</span><span class="n">event</span><span class="o">.</span><span class="n">xdata</span><span class="o">-</span><span class="bp">self</span><span class="o">.</span><span class="n">specplotter</span><span class="o">.</span><span class="n">vind</span><span class="p">))</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">nclicks_b1</span> <span class="o">+=</span> <span class="mi">1</span>
        <span class="k">elif</span> <span class="bp">self</span><span class="o">.</span><span class="n">nclicks_b1</span> <span class="o">==</span> <span class="mi">1</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">gx2</span> <span class="o">=</span> <span class="n">argmin</span><span class="p">(</span><span class="nb">abs</span><span class="p">(</span><span class="n">event</span><span class="o">.</span><span class="n">xdata</span><span class="o">-</span><span class="bp">self</span><span class="o">.</span><span class="n">specplotter</span><span class="o">.</span><span class="n">vind</span><span class="p">))</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">nclicks_b1</span> <span class="o">-=</span> <span class="mi">1</span>
            <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">gx1</span> <span class="o">&gt;</span> <span class="bp">self</span><span class="o">.</span><span class="n">gx2</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">gx1</span><span class="p">,</span><span class="bp">self</span><span class="o">.</span><span class="n">gx2</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">gx2</span><span class="p">,</span><span class="bp">self</span><span class="o">.</span><span class="n">gx1</span>
            <span class="k">if</span> <span class="nb">abs</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">gx1</span><span class="o">-</span><span class="bp">self</span><span class="o">.</span><span class="n">gx2</span><span class="p">)</span> <span class="o">&gt;</span> <span class="mi">3</span><span class="p">:</span> <span class="c"># can&#39;t fit w/ fewer data than pars</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">fitregion</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">specplotter</span><span class="o">.</span><span class="n">axis</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span>
                        <span class="bp">self</span><span class="o">.</span><span class="n">specplotter</span><span class="o">.</span><span class="n">vind</span><span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">gx1</span><span class="p">:</span><span class="bp">self</span><span class="o">.</span><span class="n">gx2</span><span class="p">],</span>
                        <span class="bp">self</span><span class="o">.</span><span class="n">specplotter</span><span class="o">.</span><span class="n">spectrum</span><span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">gx1</span><span class="p">:</span><span class="bp">self</span><span class="o">.</span><span class="n">gx2</span><span class="p">]</span><span class="o">+</span><span class="bp">self</span><span class="o">.</span><span class="n">specplotter</span><span class="o">.</span><span class="n">offset</span><span class="p">,</span>
                        <span class="n">drawstyle</span><span class="o">=</span><span class="s">&#39;steps-mid&#39;</span><span class="p">,</span>
                        <span class="n">color</span><span class="o">=</span><span class="s">&#39;c&#39;</span><span class="p">)</span>
                <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">guesses</span> <span class="o">==</span> <span class="p">[]:</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">guesses</span> <span class="o">=</span> <span class="n">gaussfitter</span><span class="o">.</span><span class="n">onedmoments</span><span class="p">(</span>
                            <span class="bp">self</span><span class="o">.</span><span class="n">specplotter</span><span class="o">.</span><span class="n">vind</span><span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">gx1</span><span class="p">:</span><span class="bp">self</span><span class="o">.</span><span class="n">gx2</span><span class="p">],</span>
                            <span class="bp">self</span><span class="o">.</span><span class="n">spectofit</span><span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">gx1</span><span class="p">:</span><span class="bp">self</span><span class="o">.</span><span class="n">gx2</span><span class="p">],</span>
                            <span class="n">vheight</span><span class="o">=</span><span class="mi">0</span><span class="p">)</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">ngauss</span> <span class="o">=</span> <span class="mi">1</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">auto</span> <span class="o">=</span> <span class="bp">True</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="k">print</span> <span class="s">&quot;Fitting region is too small (channels </span><span class="si">%i</span><span class="s">:</span><span class="si">%i</span><span class="s">).  Try again.&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">gx1</span><span class="p">,</span><span class="bp">self</span><span class="o">.</span><span class="n">gx2</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">guesspeakwidth</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span><span class="n">event</span><span class="p">):</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">nclicks_b2</span> <span class="o">%</span> <span class="mi">2</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
            <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">auto</span><span class="p">:</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">guesses</span><span class="p">[:</span><span class="mi">2</span><span class="p">]</span> <span class="o">=</span> <span class="p">[</span><span class="n">event</span><span class="o">.</span><span class="n">ydata</span><span class="p">,</span><span class="n">event</span><span class="o">.</span><span class="n">xdata</span><span class="p">]</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">guesses</span> <span class="o">+=</span> <span class="p">[</span><span class="n">event</span><span class="o">.</span><span class="n">ydata</span><span class="p">,</span><span class="n">event</span><span class="o">.</span><span class="n">xdata</span><span class="p">,</span><span class="mi">1</span><span class="p">]</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">ngauss</span> <span class="o">+=</span> <span class="mi">1</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">nclicks_b2</span> <span class="o">+=</span> <span class="mi">1</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">guessplot</span> <span class="o">+=</span> <span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">specplotter</span><span class="o">.</span><span class="n">axis</span><span class="o">.</span><span class="n">scatter</span><span class="p">(</span><span class="n">event</span><span class="o">.</span><span class="n">xdata</span><span class="p">,</span><span class="n">event</span><span class="o">.</span><span class="n">ydata</span><span class="p">,</span><span class="n">marker</span><span class="o">=</span><span class="s">&#39;x&#39;</span><span class="p">,</span><span class="n">c</span><span class="o">=</span><span class="s">&#39;r&#39;</span><span class="p">)]</span>
        <span class="k">elif</span> <span class="bp">self</span><span class="o">.</span><span class="n">nclicks_b2</span> <span class="o">%</span> <span class="mi">2</span> <span class="o">==</span> <span class="mi">1</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">guesses</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span> <span class="o">=</span> <span class="nb">abs</span><span class="p">(</span><span class="n">event</span><span class="o">.</span><span class="n">xdata</span><span class="o">-</span><span class="bp">self</span><span class="o">.</span><span class="n">guesses</span><span class="p">[</span><span class="o">-</span><span class="mi">2</span><span class="p">])</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">nclicks_b2</span> <span class="o">+=</span> <span class="mi">1</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">guessplot</span> <span class="o">+=</span> <span class="bp">self</span><span class="o">.</span><span class="n">specplotter</span><span class="o">.</span><span class="n">axis</span><span class="o">.</span><span class="n">plot</span><span class="p">([</span><span class="n">event</span><span class="o">.</span><span class="n">xdata</span><span class="p">,</span>
                <span class="mi">2</span><span class="o">*</span><span class="bp">self</span><span class="o">.</span><span class="n">guesses</span><span class="p">[</span><span class="o">-</span><span class="mi">2</span><span class="p">]</span><span class="o">-</span><span class="n">event</span><span class="o">.</span><span class="n">xdata</span><span class="p">],[</span><span class="n">event</span><span class="o">.</span><span class="n">ydata</span><span class="p">]</span><span class="o">*</span><span class="mi">2</span><span class="p">,</span>
                <span class="n">color</span><span class="o">=</span><span class="s">&#39;r&#39;</span><span class="p">)</span>
            <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">auto</span><span class="p">:</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">auto</span> <span class="o">=</span> <span class="bp">False</span>
            <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">nclicks_b2</span> <span class="o">/</span> <span class="mi">2</span> <span class="o">&gt;</span> <span class="bp">self</span><span class="o">.</span><span class="n">ngauss</span><span class="p">:</span>
                <span class="k">print</span> <span class="s">&quot;There have been </span><span class="si">%i</span><span class="s"> middle-clicks but there are only </span><span class="si">%i</span><span class="s"> gaussians&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">nclicks_b2</span><span class="p">,</span><span class="bp">self</span><span class="o">.</span><span class="n">ngauss</span><span class="p">)</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">ngauss</span> <span class="o">+=</span> <span class="mi">1</span>

    <span class="k">def</span> <span class="nf">clear</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span><span class="n">legend</span><span class="o">=</span><span class="bp">True</span><span class="p">):</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">modelplot</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">None</span><span class="p">:</span>
            <span class="k">for</span> <span class="n">p</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">modelplot</span><span class="p">:</span>
                <span class="n">p</span><span class="o">.</span><span class="n">set_visible</span><span class="p">(</span><span class="bp">False</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">legend</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">clearlegend</span><span class="p">()</span>

    <span class="k">def</span> <span class="nf">makeguess</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span><span class="n">event</span><span class="p">):</span>
        <span class="k">if</span> <span class="n">event</span><span class="o">.</span><span class="n">button</span> <span class="o">==</span> <span class="mi">1</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">selectregion</span><span class="p">(</span><span class="n">event</span><span class="p">)</span>
        <span class="k">elif</span> <span class="n">event</span><span class="o">.</span><span class="n">button</span> <span class="o">==</span> <span class="mi">2</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">guesspeakwidth</span><span class="p">(</span><span class="n">event</span><span class="p">)</span>
        <span class="k">elif</span> <span class="n">event</span><span class="o">.</span><span class="n">button</span> <span class="o">==</span> <span class="mi">3</span><span class="p">:</span>
            <span class="n">disconnect</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">click</span><span class="p">)</span>
            <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">ngauss</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">:</span>
                <span class="k">print</span> <span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">guesses</span><span class="p">)</span><span class="o">/</span><span class="mi">3</span><span class="p">,</span><span class="s">&quot; Guesses: &quot;</span><span class="p">,</span><span class="bp">self</span><span class="o">.</span><span class="n">guesses</span><span class="p">,</span><span class="s">&quot; X channel range: &quot;</span><span class="p">,</span><span class="bp">self</span><span class="o">.</span><span class="n">gx1</span><span class="p">,</span><span class="bp">self</span><span class="o">.</span><span class="n">gx2</span>
                <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">guesses</span><span class="p">)</span> <span class="o">%</span> <span class="mi">3</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">multifit</span><span class="p">()</span>
                    <span class="k">for</span> <span class="n">p</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">guessplot</span> <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">fitregion</span><span class="p">:</span>
                        <span class="n">p</span><span class="o">.</span><span class="n">set_visible</span><span class="p">(</span><span class="bp">False</span><span class="p">)</span>
                <span class="k">else</span><span class="p">:</span> 
                    <span class="k">print</span> <span class="s">&quot;error, wrong # of pars&quot;</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">specplotter</span><span class="o">.</span><span class="n">autorefresh</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">specplotter</span><span class="o">.</span><span class="n">refresh</span><span class="p">()</span>

    <span class="k">def</span> <span class="nf">clearlegend</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">gaussleg</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">None</span><span class="p">:</span> 
            <span class="bp">self</span><span class="o">.</span><span class="n">gaussleg</span><span class="o">.</span><span class="n">set_visible</span><span class="p">(</span><span class="bp">False</span><span class="p">)</span>
            <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">gaussleg</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">specplotter</span><span class="o">.</span><span class="n">axis</span><span class="o">.</span><span class="n">artists</span><span class="p">:</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">specplotter</span><span class="o">.</span><span class="n">axis</span><span class="o">.</span><span class="n">artists</span><span class="o">.</span><span class="n">remove</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">gaussleg</span><span class="p">)</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">specplotter</span><span class="o">.</span><span class="n">autorefresh</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">specplotter</span><span class="o">.</span><span class="n">refresh</span><span class="p">()</span>
    
    <span class="k">def</span> <span class="nf">savefit</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">modelpars</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">None</span><span class="p">:</span>
            <span class="k">for</span> <span class="n">ii</span><span class="p">,</span><span class="n">p</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">modelpars</span><span class="p">):</span>
                <span class="k">if</span> <span class="n">ii</span> <span class="o">%</span> <span class="mi">3</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">specplotter</span><span class="o">.</span><span class="n">header</span><span class="o">.</span><span class="n">update</span><span class="p">(</span><span class="s">&#39;AMP</span><span class="si">%1i</span><span class="s">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="n">ii</span><span class="o">/</span><span class="mi">3</span><span class="p">),</span><span class="n">p</span><span class="p">,</span><span class="n">comment</span><span class="o">=</span><span class="s">&quot;Gaussian best fit amplitude #</span><span class="si">%i</span><span class="s">&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="n">ii</span><span class="o">/</span><span class="mi">3</span><span class="p">))</span>
                <span class="k">if</span> <span class="n">ii</span> <span class="o">%</span> <span class="mi">3</span> <span class="o">==</span> <span class="mi">1</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">specplotter</span><span class="o">.</span><span class="n">header</span><span class="o">.</span><span class="n">update</span><span class="p">(</span><span class="s">&#39;CEN</span><span class="si">%1i</span><span class="s">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="n">ii</span><span class="o">/</span><span class="mi">3</span><span class="p">),</span><span class="n">p</span><span class="p">,</span><span class="n">comment</span><span class="o">=</span><span class="s">&quot;Gaussian best fit center #</span><span class="si">%i</span><span class="s">&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="n">ii</span><span class="o">/</span><span class="mi">3</span><span class="p">))</span>
                <span class="k">if</span> <span class="n">ii</span> <span class="o">%</span> <span class="mi">3</span> <span class="o">==</span> <span class="mi">2</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">specplotter</span><span class="o">.</span><span class="n">header</span><span class="o">.</span><span class="n">update</span><span class="p">(</span><span class="s">&#39;WID</span><span class="si">%1i</span><span class="s">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="n">ii</span><span class="o">/</span><span class="mi">3</span><span class="p">),</span><span class="n">p</span><span class="p">,</span><span class="n">comment</span><span class="o">=</span><span class="s">&quot;Gaussian best fit width #</span><span class="si">%i</span><span class="s">&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="n">ii</span><span class="o">/</span><span class="mi">3</span><span class="p">))</span>


<span class="k">def</span> <span class="nf">mapplot</span><span class="p">(</span><span class="n">plane</span><span class="p">,</span><span class="n">cube</span><span class="p">,</span><span class="n">vconv</span><span class="o">=</span><span class="k">lambda</span> <span class="n">x</span><span class="p">:</span> <span class="n">x</span><span class="p">,</span><span class="n">xtora</span><span class="o">=</span><span class="k">lambda</span> <span class="n">x</span><span class="p">:</span> <span class="n">x</span><span class="p">,</span><span class="n">ytodec</span><span class="o">=</span><span class="k">lambda</span> <span class="n">x</span><span class="p">:</span> <span class="n">x</span><span class="p">,</span> <span class="n">gaiafignum</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span> <span class="n">specfignum</span><span class="o">=</span><span class="mi">1</span><span class="p">):</span>
    
    <span class="n">gaiafig</span> <span class="o">=</span> <span class="n">figure</span><span class="p">(</span><span class="n">gaiafignum</span><span class="p">)</span>
    <span class="n">gaiafig</span><span class="o">.</span><span class="n">clf</span><span class="p">()</span>
    <span class="n">gaiaax</span> <span class="o">=</span> <span class="n">gaiafig</span><span class="o">.</span><span class="n">add_subplot</span><span class="p">(</span><span class="mi">111</span><span class="p">)</span>
    <span class="n">gaiaax</span><span class="o">.</span><span class="n">imshow</span><span class="p">(</span><span class="n">plane</span><span class="p">)</span>

    <span class="n">sp</span> <span class="o">=</span> <span class="n">SpecPlotter</span><span class="p">(</span><span class="n">cube</span><span class="p">,</span> <span class="n">vconv</span><span class="o">=</span><span class="n">vconv</span><span class="p">,</span> <span class="n">xtora</span><span class="o">=</span><span class="n">xtora</span><span class="p">,</span> <span class="n">ytodec</span><span class="o">=</span><span class="n">ytodec</span><span class="p">,</span>
            <span class="n">gaiafignum</span><span class="o">=</span><span class="n">gaiafignum</span><span class="p">,</span> <span class="n">fignum</span><span class="o">=</span><span class="n">specfignum</span><span class="p">,</span> <span class="n">gaiafig</span><span class="o">=</span><span class="n">gaiafig</span><span class="p">)</span>
    <span class="n">sp</span><span class="o">.</span><span class="n">clickid</span> <span class="o">=</span> <span class="n">gaiafig</span><span class="o">.</span><span class="n">canvas</span><span class="o">.</span><span class="n">mpl_connect</span><span class="p">(</span><span class="s">&#39;button_press_event&#39;</span><span class="p">,</span><span class="n">sp</span><span class="p">)</span>
    <span class="c">#connect(&#39;button_press_event&#39;,sp)</span>

 
<div class="viewcode-block" id="splat_3d"><a class="viewcode-back" href="../../agpy.html#agpy.showspec.splat_3d">[docs]</a><span class="k">def</span> <span class="nf">splat_3d</span><span class="p">(</span><span class="n">filename</span><span class="p">,</span><span class="n">xi</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span><span class="n">yi</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span><span class="n">vmin</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span><span class="n">vmax</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span><span class="n">button</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span><span class="n">dobaseline</span><span class="o">=</span><span class="bp">False</span><span class="p">,</span><span class="n">exclude</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span>
        <span class="n">smooth</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span><span class="n">smoothto</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span><span class="n">smoothtype</span><span class="o">=</span><span class="s">&#39;gaussian&#39;</span><span class="p">,</span><span class="n">order</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span><span class="n">savepre</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span><span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Inputs:</span>
<span class="sd">        vmin,vmax - range over which to baseline and plottransform = ax.transAxes</span>
<span class="sd">        exclude - (internal) range to exclude from baseline fit</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">dv</span><span class="p">,</span><span class="n">v0</span><span class="p">,</span><span class="n">p3</span><span class="p">,</span><span class="n">hdr</span><span class="p">,</span><span class="n">cube</span><span class="p">,</span><span class="n">xtora</span><span class="p">,</span><span class="n">ytodec</span><span class="p">,</span><span class="n">vconv</span><span class="p">,</span><span class="n">xunits</span><span class="p">,</span><span class="n">conversion_factor</span><span class="p">,</span><span class="n">units</span> <span class="o">=</span> <span class="n">open_3d</span><span class="p">(</span><span class="n">filename</span><span class="p">)</span>

    <span class="k">if</span> <span class="n">units</span> <span class="ow">is</span> <span class="bp">None</span><span class="p">:</span> <span class="n">units</span><span class="o">=</span><span class="s">&quot;UNITS&quot;</span>
    <span class="k">if</span> <span class="n">xunits</span> <span class="ow">is</span> <span class="bp">None</span><span class="p">:</span> <span class="n">xunits</span><span class="o">=</span><span class="s">&quot;km/s&quot;</span>
    <span class="k">if</span> <span class="n">conversion_factor</span> <span class="o">==</span> <span class="mi">0</span> <span class="ow">or</span> <span class="n">conversion_factor</span> <span class="ow">is</span> <span class="bp">None</span><span class="p">:</span> <span class="n">conversion_factor</span><span class="o">=</span><span class="mf">1.0</span>
    <span class="n">sp</span> <span class="o">=</span> <span class="n">splat_1d</span><span class="p">(</span><span class="n">vpars</span><span class="o">=</span><span class="p">[</span><span class="n">dv</span><span class="p">,</span> <span class="n">v0</span><span class="p">,</span> <span class="n">p3</span><span class="p">],</span> <span class="n">hdr</span><span class="o">=</span><span class="n">hdr</span><span class="p">,</span> <span class="n">spec</span><span class="o">=</span><span class="n">cube</span><span class="p">[:,</span> <span class="n">yi</span><span class="p">,</span> <span class="n">xi</span><span class="p">],</span>
            <span class="n">xtora</span><span class="o">=</span><span class="n">xtora</span><span class="p">,</span> <span class="n">ytodec</span><span class="o">=</span><span class="n">ytodec</span><span class="p">,</span> <span class="n">vconv</span><span class="o">=</span><span class="n">vconv</span><span class="p">,</span> <span class="n">units</span><span class="o">=</span><span class="n">units</span><span class="p">,</span>
            <span class="n">conversion_factor</span><span class="o">=</span><span class="n">conversion_factor</span><span class="p">,</span> <span class="n">xunits</span><span class="o">=</span><span class="n">xunits</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>

    <span class="n">sp</span><span class="o">.</span><span class="n">cube</span> <span class="o">=</span> <span class="n">cube</span>

    <span class="k">return</span> <span class="n">sp</span>
</div>
<span class="k">def</span> <span class="nf">gaia</span><span class="p">(</span><span class="n">filename</span><span class="p">,</span><span class="n">estimator</span><span class="o">=</span><span class="s">&#39;max&#39;</span><span class="p">,</span><span class="n">axis</span><span class="o">=</span><span class="mi">0</span><span class="p">):</span>
    <span class="n">f</span> <span class="o">=</span> <span class="n">pyfits</span><span class="o">.</span><span class="n">open</span><span class="p">(</span><span class="n">filename</span><span class="p">)</span>
    <span class="n">hdr</span> <span class="o">=</span> <span class="n">f</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">header</span>
    <span class="n">cube</span> <span class="o">=</span> <span class="n">f</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">data</span>
    <span class="n">dv</span><span class="p">,</span><span class="n">v0</span><span class="p">,</span><span class="n">p3</span> <span class="o">=</span> <span class="n">hdr</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s">&#39;CD3_3&#39;</span><span class="p">),</span><span class="n">hdr</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s">&#39;CRVAL3&#39;</span><span class="p">),</span><span class="n">hdr</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s">&#39;CRPIX3&#39;</span><span class="p">)</span>
    <span class="n">dr</span><span class="p">,</span><span class="n">r0</span><span class="p">,</span><span class="n">p1</span> <span class="o">=</span> <span class="n">hdr</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s">&#39;CD1_1&#39;</span><span class="p">),</span><span class="n">hdr</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s">&#39;CRVAL1&#39;</span><span class="p">),</span><span class="n">hdr</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s">&#39;CRPIX1&#39;</span><span class="p">)</span>
    <span class="n">dd</span><span class="p">,</span><span class="n">d0</span><span class="p">,</span><span class="n">p2</span> <span class="o">=</span> <span class="n">hdr</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s">&#39;CD2_2&#39;</span><span class="p">),</span><span class="n">hdr</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s">&#39;CRVAL2&#39;</span><span class="p">),</span><span class="n">hdr</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s">&#39;CRPIX2&#39;</span><span class="p">)</span>
    <span class="k">if</span> <span class="n">dv</span> <span class="ow">is</span> <span class="bp">None</span><span class="p">:</span> <span class="n">dv</span> <span class="o">=</span> <span class="n">hdr</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s">&#39;CDELT3&#39;</span><span class="p">)</span>
    <span class="k">if</span> <span class="n">dr</span> <span class="ow">is</span> <span class="bp">None</span><span class="p">:</span> <span class="n">dr</span> <span class="o">=</span> <span class="n">hdr</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s">&#39;CDELT1&#39;</span><span class="p">)</span>
    <span class="k">if</span> <span class="n">dd</span> <span class="ow">is</span> <span class="bp">None</span><span class="p">:</span> <span class="n">dd</span> <span class="o">=</span> <span class="n">hdr</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s">&#39;CDELT2&#39;</span><span class="p">)</span>
    <span class="n">xtora</span> <span class="o">=</span> <span class="k">lambda</span> <span class="n">x</span><span class="p">:</span> <span class="p">(</span><span class="n">x</span><span class="o">-</span><span class="n">p1</span><span class="o">+</span><span class="mi">1</span><span class="p">)</span><span class="o">*</span><span class="n">dr</span><span class="o">+</span><span class="n">r0</span>    <span class="c"># convert pixel coordinates to RA/Dec/Velocity</span>
    <span class="n">ytodec</span> <span class="o">=</span> <span class="k">lambda</span> <span class="n">y</span><span class="p">:</span> <span class="p">(</span><span class="n">y</span><span class="o">-</span><span class="n">p2</span><span class="o">+</span><span class="mi">1</span><span class="p">)</span><span class="o">*</span><span class="n">dd</span><span class="o">+</span><span class="n">d0</span>
    <span class="n">vconv</span> <span class="o">=</span> <span class="k">lambda</span> <span class="n">v</span><span class="p">:</span> <span class="p">(</span><span class="n">v</span><span class="o">-</span><span class="n">p3</span><span class="o">+</span><span class="mi">1</span><span class="p">)</span><span class="o">*</span><span class="n">dv</span><span class="o">+</span><span class="n">v0</span>

    <span class="k">if</span> <span class="n">axis</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">:</span>
        <span class="n">cube</span> <span class="o">=</span> <span class="n">cube</span><span class="o">.</span><span class="n">swapaxes</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span><span class="n">axis</span><span class="p">)</span>

    <span class="k">if</span> <span class="n">estimator</span> <span class="o">==</span> <span class="s">&#39;max&#39;</span><span class="p">:</span>
        <span class="n">p</span> <span class="o">=</span> <span class="n">where</span><span class="p">(</span><span class="n">isnan</span><span class="p">(</span><span class="n">cube</span><span class="p">),</span><span class="mi">0</span><span class="p">,</span><span class="n">cube</span><span class="p">)</span><span class="o">.</span><span class="n">max</span><span class="p">(</span><span class="n">axis</span><span class="o">=</span><span class="mi">0</span><span class="p">)</span>
    <span class="k">elif</span> <span class="n">estimator</span> <span class="o">==</span> <span class="s">&#39;int&#39;</span><span class="p">:</span>
        <span class="n">p</span> <span class="o">=</span> <span class="n">where</span><span class="p">(</span><span class="n">isnan</span><span class="p">(</span><span class="n">cube</span><span class="p">),</span><span class="mi">0</span><span class="p">,</span><span class="n">cube</span><span class="p">)</span><span class="o">.</span><span class="n">sum</span><span class="p">(</span><span class="n">axis</span><span class="o">=</span><span class="mi">0</span><span class="p">)</span> <span class="o">*</span> <span class="n">dv</span>
    <span class="k">elif</span> <span class="n">estimator</span> <span class="o">==</span> <span class="s">&#39;intdivmax&#39;</span><span class="p">:</span>
        <span class="n">cut</span> <span class="o">=</span> <span class="n">MAD</span><span class="p">(</span><span class="n">cube</span><span class="o">.</span><span class="n">ravel</span><span class="p">())</span> <span class="o">+</span> <span class="n">nanmedian</span><span class="p">(</span><span class="n">cube</span><span class="o">.</span><span class="n">ravel</span><span class="p">())</span>
        <span class="k">if</span> <span class="n">cut</span> <span class="o">&lt;</span> <span class="mi">0</span><span class="p">:</span>
            <span class="n">cut</span> <span class="o">=</span> <span class="mi">0</span>
        <span class="n">m</span> <span class="o">=</span> <span class="n">where</span><span class="p">(</span><span class="n">isnan</span><span class="p">(</span><span class="n">cube</span><span class="p">),</span><span class="mi">0</span><span class="p">,</span><span class="n">cube</span><span class="p">)</span><span class="o">.</span><span class="n">max</span><span class="p">(</span><span class="n">axis</span><span class="o">=</span><span class="mi">0</span><span class="p">)</span>
        <span class="n">i</span> <span class="o">=</span> <span class="n">where</span><span class="p">(</span><span class="n">isnan</span><span class="p">(</span><span class="n">cube</span><span class="p">),</span><span class="mi">0</span><span class="p">,</span><span class="n">cube</span><span class="p">)</span><span class="o">.</span><span class="n">sum</span><span class="p">(</span><span class="n">axis</span><span class="o">=</span><span class="mi">0</span><span class="p">)</span> <span class="o">*</span> <span class="n">dv</span>
        <span class="n">p</span> <span class="o">=</span> <span class="n">where</span><span class="p">(</span><span class="n">i</span><span class="o">&lt;</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="n">i</span><span class="p">)</span><span class="o">/</span><span class="n">where</span><span class="p">(</span><span class="n">m</span><span class="o">&lt;=</span><span class="n">cut</span><span class="p">,</span><span class="n">numpy</span><span class="o">.</span><span class="n">inf</span><span class="p">,</span><span class="n">m</span><span class="p">)</span>
    <span class="k">elif</span> <span class="n">estimator</span><span class="p">[</span><span class="o">-</span><span class="mi">5</span><span class="p">:]</span> <span class="o">==</span> <span class="s">&quot;.fits&quot;</span><span class="p">:</span>
        <span class="n">p</span> <span class="o">=</span> <span class="n">pyfits</span><span class="o">.</span><span class="n">open</span><span class="p">(</span><span class="n">estimator</span><span class="p">)[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">data</span>

    <span class="n">mapplot</span><span class="p">(</span><span class="n">p</span><span class="p">,</span><span class="n">cube</span><span class="p">,</span><span class="n">vconv</span><span class="p">,</span><span class="n">xtora</span><span class="p">,</span><span class="n">ytodec</span><span class="p">)</span>

<span class="k">def</span> <span class="nf">baseline_file</span><span class="p">(</span><span class="n">filename</span><span class="p">,</span><span class="n">outfilename</span><span class="p">,</span><span class="n">vmin</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span><span class="n">vmax</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span><span class="n">order</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span><span class="n">crop</span><span class="o">=</span><span class="bp">False</span><span class="p">):</span>
    <span class="n">f</span> <span class="o">=</span> <span class="n">pyfits</span><span class="o">.</span><span class="n">open</span><span class="p">(</span><span class="n">filename</span><span class="p">)</span>
    <span class="n">hdr</span> <span class="o">=</span> <span class="n">f</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">header</span>
    <span class="n">cube</span> <span class="o">=</span> <span class="n">f</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">data</span><span class="o">.</span><span class="n">squeeze</span><span class="p">()</span>
    <span class="n">dv</span><span class="p">,</span><span class="n">v0</span><span class="p">,</span><span class="n">p3</span> <span class="o">=</span> <span class="n">hdr</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s">&#39;CD3_3&#39;</span><span class="p">),</span><span class="n">hdr</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s">&#39;CRVAL3&#39;</span><span class="p">),</span><span class="n">hdr</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s">&#39;CRPIX3&#39;</span><span class="p">)</span>
    <span class="n">dr</span><span class="p">,</span><span class="n">r0</span><span class="p">,</span><span class="n">p1</span> <span class="o">=</span> <span class="n">hdr</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s">&#39;CD1_1&#39;</span><span class="p">),</span><span class="n">hdr</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s">&#39;CRVAL1&#39;</span><span class="p">),</span><span class="n">hdr</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s">&#39;CRPIX1&#39;</span><span class="p">)</span>
    <span class="n">dd</span><span class="p">,</span><span class="n">d0</span><span class="p">,</span><span class="n">p2</span> <span class="o">=</span> <span class="n">hdr</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s">&#39;CD2_2&#39;</span><span class="p">),</span><span class="n">hdr</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s">&#39;CRVAL2&#39;</span><span class="p">),</span><span class="n">hdr</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s">&#39;CRPIX2&#39;</span><span class="p">)</span>
    <span class="k">if</span> <span class="n">dv</span> <span class="ow">is</span> <span class="bp">None</span><span class="p">:</span> <span class="n">dv</span> <span class="o">=</span> <span class="n">hdr</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s">&#39;CDELT3&#39;</span><span class="p">)</span>
    <span class="k">if</span> <span class="n">dr</span> <span class="ow">is</span> <span class="bp">None</span><span class="p">:</span> <span class="n">dr</span> <span class="o">=</span> <span class="n">hdr</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s">&#39;CDELT1&#39;</span><span class="p">)</span>
    <span class="k">if</span> <span class="n">dd</span> <span class="ow">is</span> <span class="bp">None</span><span class="p">:</span> <span class="n">dd</span> <span class="o">=</span> <span class="n">hdr</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s">&#39;CDELT2&#39;</span><span class="p">)</span>
    <span class="n">vconv</span> <span class="o">=</span> <span class="k">lambda</span> <span class="n">v</span><span class="p">:</span> <span class="p">(</span><span class="n">v</span><span class="o">-</span><span class="n">p3</span><span class="o">+</span><span class="mi">1</span><span class="p">)</span><span class="o">*</span><span class="n">dv</span><span class="o">+</span><span class="n">v0</span>
    <span class="n">varr</span> <span class="o">=</span> <span class="n">vconv</span><span class="p">(</span><span class="n">arange</span><span class="p">(</span><span class="n">cube</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">]))</span>
    <span class="k">if</span> <span class="n">vmin</span> <span class="ow">is</span> <span class="bp">None</span><span class="p">:</span> <span class="n">argvmin</span> <span class="o">=</span> <span class="bp">None</span>
    <span class="k">else</span><span class="p">:</span> <span class="n">argvmin</span> <span class="o">=</span> <span class="n">argmin</span><span class="p">(</span><span class="nb">abs</span><span class="p">(</span><span class="n">varr</span><span class="o">-</span><span class="n">vmin</span><span class="p">))</span>
    <span class="k">if</span> <span class="n">vmax</span> <span class="ow">is</span> <span class="bp">None</span><span class="p">:</span> <span class="n">argvmax</span> <span class="o">=</span> <span class="bp">None</span>
    <span class="k">else</span><span class="p">:</span> <span class="n">argvmax</span> <span class="o">=</span> <span class="n">argmin</span><span class="p">(</span><span class="nb">abs</span><span class="p">(</span><span class="n">varr</span><span class="o">-</span><span class="n">vmax</span><span class="p">))</span>

    <span class="n">bspec</span><span class="p">,</span><span class="n">bfit</span> <span class="o">=</span> <span class="n">baseline</span><span class="p">(</span><span class="n">cube</span><span class="p">,</span><span class="n">vmin</span><span class="o">=</span><span class="n">argvmin</span><span class="p">,</span><span class="n">vmax</span><span class="o">=</span><span class="n">argvmax</span><span class="p">,</span><span class="n">order</span><span class="o">=</span><span class="n">order</span><span class="p">)</span>


<div class="viewcode-block" id="baseline"><a class="viewcode-back" href="../../agpy.html#agpy.showspec.baseline">[docs]</a><span class="k">def</span> <span class="nf">baseline</span><span class="p">(</span><span class="n">spectrum</span><span class="p">,</span><span class="n">xarr</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span><span class="n">xmin</span><span class="o">=</span><span class="s">&#39;default&#39;</span><span class="p">,</span><span class="n">xmax</span><span class="o">=</span><span class="s">&#39;default&#39;</span><span class="p">,</span><span class="n">order</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span><span class="n">quiet</span><span class="o">=</span><span class="bp">True</span><span class="p">,</span><span class="n">exclude</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span>
        <span class="n">mask</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Subtract a baseline from a spectrum</span>
<span class="sd">    If xmin,xmax are not specified, defaults to ignoring first and last 10% of spectrum</span>
<span class="sd">    *unless* order &gt; 1, in which case ignoring the ends tends to cause strange effects</span>

<span class="sd">    exclude is a set of start/end indices to ignore when baseline fitting</span>
<span class="sd">    (ignored by setting error to infinite in fitting procedure)</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">if</span> <span class="n">xmin</span> <span class="o">==</span> <span class="s">&#39;default&#39;</span><span class="p">:</span>
        <span class="k">if</span> <span class="n">order</span> <span class="o">&lt;=</span> <span class="mi">1</span><span class="p">:</span> <span class="n">xmin</span> <span class="o">=</span> <span class="n">floor</span><span class="p">(</span> <span class="n">spectrum</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span><span class="o">*</span><span class="mf">0.1</span> <span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>          <span class="n">xmin</span> <span class="o">=</span> <span class="mi">0</span>
    <span class="k">elif</span> <span class="n">xmin</span> <span class="ow">is</span> <span class="bp">None</span><span class="p">:</span>
        <span class="n">xmin</span> <span class="o">=</span> <span class="mi">0</span>
    <span class="k">if</span> <span class="n">xmax</span> <span class="o">==</span> <span class="s">&#39;default&#39;</span><span class="p">:</span>
        <span class="k">if</span> <span class="n">order</span> <span class="o">&lt;=</span> <span class="mi">1</span><span class="p">:</span> <span class="n">xmax</span> <span class="o">=</span> <span class="n">ceil</span><span class="p">(</span> <span class="n">spectrum</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span><span class="o">*</span><span class="mf">0.9</span> <span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>          <span class="n">xmax</span> <span class="o">=</span> <span class="n">spectrum</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span>
    <span class="k">elif</span> <span class="n">xmax</span> <span class="ow">is</span> <span class="bp">None</span><span class="p">:</span>
        <span class="n">xmax</span> <span class="o">=</span> <span class="n">spectrum</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span>
    
    <span class="n">pguess</span> <span class="o">=</span> <span class="p">[</span><span class="mi">1</span><span class="p">]</span><span class="o">*</span><span class="p">(</span><span class="n">order</span><span class="o">+</span><span class="mi">1</span><span class="p">)</span>

    <span class="k">if</span> <span class="n">xarr</span> <span class="ow">is</span> <span class="bp">None</span><span class="p">:</span>
        <span class="n">xarr</span> <span class="o">=</span> <span class="n">indices</span><span class="p">(</span><span class="n">spectrum</span><span class="o">.</span><span class="n">shape</span><span class="p">)</span><span class="o">.</span><span class="n">squeeze</span><span class="p">()</span>

    <span class="n">subxarr</span> <span class="o">=</span> <span class="n">xarr</span><span class="p">[</span><span class="n">xmin</span><span class="p">:</span><span class="n">xmax</span><span class="p">]</span>
    <span class="k">def</span> <span class="nf">mpfitfun</span><span class="p">(</span><span class="n">data</span><span class="p">,</span><span class="n">err</span><span class="p">):</span>
        <span class="k">def</span> <span class="nf">f</span><span class="p">(</span><span class="n">p</span><span class="p">,</span><span class="n">fjac</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span> <span class="k">return</span> <span class="p">[</span><span class="mi">0</span><span class="p">,</span><span class="n">numpy</span><span class="o">.</span><span class="n">ravel</span><span class="p">((</span><span class="n">poly1d</span><span class="p">(</span><span class="n">p</span><span class="p">)(</span><span class="n">subxarr</span><span class="p">)</span><span class="o">-</span><span class="n">data</span><span class="p">)</span><span class="o">/</span><span class="n">err</span><span class="p">)]</span>
        <span class="k">return</span> <span class="n">f</span>

    <span class="n">err</span> <span class="o">=</span> <span class="n">ones</span><span class="p">(</span><span class="n">spectrum</span><span class="o">.</span><span class="n">shape</span><span class="p">)</span>
    <span class="k">if</span> <span class="n">exclude</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">None</span><span class="p">:</span>
        <span class="n">err</span><span class="p">[</span><span class="n">exclude</span><span class="p">[</span><span class="mi">0</span><span class="p">]:</span><span class="n">exclude</span><span class="p">[</span><span class="mi">1</span><span class="p">]]</span> <span class="o">=</span> <span class="mf">1e10</span>
    <span class="k">if</span> <span class="n">mask</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">None</span><span class="p">:</span>
        <span class="k">if</span> <span class="n">mask</span><span class="o">.</span><span class="n">dtype</span><span class="o">.</span><span class="n">name</span> <span class="o">!=</span> <span class="s">&#39;bool&#39;</span><span class="p">:</span> <span class="n">mask</span> <span class="o">=</span> <span class="n">mask</span><span class="o">.</span><span class="n">astype</span><span class="p">(</span><span class="s">&#39;bool&#39;</span><span class="p">)</span>
        <span class="n">err</span><span class="p">[</span><span class="n">mask</span><span class="p">]</span> <span class="o">=</span> <span class="mf">1e10</span>
        <span class="n">spectrum</span><span class="p">[</span><span class="n">mask</span><span class="p">]</span> <span class="o">=</span> <span class="mi">0</span>
    <span class="k">if</span> <span class="p">(</span><span class="n">spectrum</span><span class="o">!=</span><span class="n">spectrum</span><span class="p">)</span><span class="o">.</span><span class="n">sum</span><span class="p">()</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">:</span>
        <span class="k">print</span> <span class="s">&quot;There is an error in baseline: some values are NaN&quot;</span>
        <span class="kn">import</span> <span class="nn">pdb</span><span class="p">;</span> <span class="n">pdb</span><span class="o">.</span><span class="n">set_trace</span><span class="p">()</span>

    <span class="n">mp</span> <span class="o">=</span> <span class="n">mpfit</span><span class="p">(</span><span class="n">mpfitfun</span><span class="p">(</span><span class="n">spectrum</span><span class="p">[</span><span class="n">xmin</span><span class="p">:</span><span class="n">xmax</span><span class="p">],</span><span class="n">err</span><span class="p">[</span><span class="n">xmin</span><span class="p">:</span><span class="n">xmax</span><span class="p">]),</span><span class="n">xall</span><span class="o">=</span><span class="n">pguess</span><span class="p">,</span><span class="n">quiet</span><span class="o">=</span><span class="n">quiet</span><span class="p">)</span>
    <span class="n">fitp</span> <span class="o">=</span> <span class="n">mp</span><span class="o">.</span><span class="n">params</span>
    <span class="n">bestfit</span> <span class="o">=</span> <span class="n">poly1d</span><span class="p">(</span><span class="n">fitp</span><span class="p">)(</span><span class="n">xarr</span><span class="p">)</span><span class="o">.</span><span class="n">squeeze</span><span class="p">()</span>

    <span class="k">return</span> <span class="p">(</span><span class="n">spectrum</span><span class="o">-</span><span class="n">bestfit</span><span class="p">),</span><span class="n">fitp</span>
</div>
<span class="k">def</span> <span class="nf">open_3d</span><span class="p">(</span><span class="n">filename</span><span class="p">):</span>
    <span class="n">f</span> <span class="o">=</span> <span class="n">pyfits</span><span class="o">.</span><span class="n">open</span><span class="p">(</span><span class="n">filename</span><span class="p">)</span>
    <span class="n">hdr</span> <span class="o">=</span> <span class="n">f</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">header</span>
    <span class="n">cube</span> <span class="o">=</span> <span class="n">f</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">data</span>
    <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">cube</span><span class="o">.</span><span class="n">shape</span><span class="p">)</span> <span class="o">==</span> <span class="mi">4</span><span class="p">:</span> <span class="n">cube</span><span class="o">=</span><span class="n">cube</span><span class="p">[</span><span class="mi">0</span><span class="p">,:,:,:]</span>
    <span class="c">#cube = reshape(cube.mean(axis=2).mean(axis=1),[cube.shape[0],1,1])</span>
    <span class="n">dv</span><span class="p">,</span><span class="n">v0</span><span class="p">,</span><span class="n">p3</span> <span class="o">=</span> <span class="n">hdr</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s">&#39;CD3_3&#39;</span><span class="p">),</span><span class="n">hdr</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s">&#39;CRVAL3&#39;</span><span class="p">),</span><span class="n">hdr</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s">&#39;CRPIX3&#39;</span><span class="p">)</span>
    <span class="n">dr</span><span class="p">,</span><span class="n">r0</span><span class="p">,</span><span class="n">p1</span> <span class="o">=</span> <span class="n">hdr</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s">&#39;CD1_1&#39;</span><span class="p">),</span><span class="n">hdr</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s">&#39;CRVAL1&#39;</span><span class="p">),</span><span class="n">hdr</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s">&#39;CRPIX1&#39;</span><span class="p">)</span>
    <span class="n">dd</span><span class="p">,</span><span class="n">d0</span><span class="p">,</span><span class="n">p2</span> <span class="o">=</span> <span class="n">hdr</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s">&#39;CD2_2&#39;</span><span class="p">),</span><span class="n">hdr</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s">&#39;CRVAL2&#39;</span><span class="p">),</span><span class="n">hdr</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s">&#39;CRPIX2&#39;</span><span class="p">)</span>
    <span class="k">if</span> <span class="n">dv</span> <span class="ow">is</span> <span class="bp">None</span><span class="p">:</span> <span class="n">dv</span> <span class="o">=</span> <span class="n">hdr</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s">&#39;CDELT3&#39;</span><span class="p">)</span>
    <span class="k">if</span> <span class="n">dr</span> <span class="ow">is</span> <span class="bp">None</span><span class="p">:</span> <span class="n">dr</span> <span class="o">=</span> <span class="n">hdr</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s">&#39;CDELT1&#39;</span><span class="p">)</span>
    <span class="k">if</span> <span class="n">dd</span> <span class="ow">is</span> <span class="bp">None</span><span class="p">:</span> <span class="n">dd</span> <span class="o">=</span> <span class="n">hdr</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s">&#39;CDELT2&#39;</span><span class="p">)</span>
    <span class="n">xtora</span> <span class="o">=</span> <span class="k">lambda</span> <span class="n">x</span><span class="p">:</span> <span class="p">(</span><span class="n">x</span><span class="o">-</span><span class="n">p1</span><span class="o">+</span><span class="mi">1</span><span class="p">)</span><span class="o">*</span><span class="n">dr</span><span class="o">+</span><span class="n">r0</span>    <span class="c"># convert pixel coordinates to RA/Dec/Velocity</span>
    <span class="n">ytodec</span> <span class="o">=</span> <span class="k">lambda</span> <span class="n">y</span><span class="p">:</span> <span class="p">(</span><span class="n">y</span><span class="o">-</span><span class="n">p2</span><span class="o">+</span><span class="mi">1</span><span class="p">)</span><span class="o">*</span><span class="n">dd</span><span class="o">+</span><span class="n">d0</span>
    <span class="n">vconv</span> <span class="o">=</span> <span class="k">lambda</span> <span class="n">v</span><span class="p">:</span> <span class="p">(</span><span class="n">v</span><span class="o">-</span><span class="n">p3</span><span class="o">+</span><span class="mi">1</span><span class="p">)</span><span class="o">*</span><span class="n">dv</span><span class="o">+</span><span class="n">v0</span>
    <span class="k">if</span> <span class="n">hdr</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s">&#39;CUNIT3&#39;</span><span class="p">)</span> <span class="ow">in</span> <span class="p">[</span><span class="s">&#39;m/s&#39;</span><span class="p">,</span><span class="s">&#39;M/S&#39;</span><span class="p">]:</span>
        <span class="n">conversion_factor</span> <span class="o">=</span> <span class="mf">1000.0</span>
        <span class="n">xunits</span> <span class="o">=</span> <span class="s">&#39;km/s&#39;</span> <span class="c"># change to km/s because you&#39;re converting units</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="n">xunits</span> <span class="o">=</span> <span class="n">hdr</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s">&#39;CUNIT3&#39;</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">xunits</span> <span class="ow">in</span> <span class="p">(</span><span class="s">&quot;hz&quot;</span><span class="p">,</span><span class="s">&quot;Hz&quot;</span><span class="p">):</span>
            <span class="k">print</span> <span class="s">&quot;Converting from Hz to GHz&quot;</span>
            <span class="n">xunits</span> <span class="o">=</span> <span class="s">&quot;GHz&quot;</span>
            <span class="n">conversion_factor</span> <span class="o">=</span> <span class="mf">1.0e9</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">conversion_factor</span> <span class="o">=</span> <span class="mf">1.0</span>
    <span class="n">units</span> <span class="o">=</span> <span class="n">hdr</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s">&#39;BUNIT&#39;</span><span class="p">)</span>

    <span class="k">return</span> <span class="n">dv</span><span class="p">,</span><span class="n">v0</span><span class="p">,</span><span class="n">p3</span><span class="p">,</span><span class="n">hdr</span><span class="p">,</span><span class="n">cube</span><span class="p">,</span><span class="n">xtora</span><span class="p">,</span><span class="n">ytodec</span><span class="p">,</span><span class="n">vconv</span><span class="p">,</span><span class="n">xunits</span><span class="p">,</span><span class="n">conversion_factor</span><span class="p">,</span><span class="n">units</span>

<div class="viewcode-block" id="open_1d"><a class="viewcode-back" href="../../agpy.html#agpy.showspec.open_1d">[docs]</a><span class="k">def</span> <span class="nf">open_1d</span><span class="p">(</span><span class="n">filename</span><span class="p">,</span><span class="n">specnum</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span><span class="n">wcstype</span><span class="o">=</span><span class="s">&#39;&#39;</span><span class="p">,</span><span class="n">errspecnum</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span><span class="n">maskspecnum</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Grabs all the relevant pieces of a 1d spectrum for plotting</span>
<span class="sd">    wcstype is the suffix on the WCS type to get to velocity/frequency/whatever</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">f</span> <span class="o">=</span> <span class="n">pyfits</span><span class="o">.</span><span class="n">open</span><span class="p">(</span><span class="n">filename</span><span class="p">)</span>
    <span class="n">hdr</span> <span class="o">=</span> <span class="n">f</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">header</span>
    <span class="n">spec</span> <span class="o">=</span> <span class="n">f</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">data</span>
    <span class="n">errspec</span>  <span class="o">=</span> <span class="bp">None</span>
    <span class="n">maskspec</span> <span class="o">=</span> <span class="bp">None</span>
    <span class="k">if</span> <span class="n">hdr</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s">&#39;NAXIS&#39;</span><span class="p">)</span> <span class="o">==</span> <span class="mi">2</span><span class="p">:</span>
        <span class="k">if</span> <span class="n">errspecnum</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">None</span><span class="p">:</span>
            <span class="n">errspec</span> <span class="o">=</span> <span class="n">spec</span><span class="p">[</span><span class="n">errspecnum</span><span class="p">,:]</span>
        <span class="k">if</span> <span class="n">maskspecnum</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">None</span><span class="p">:</span>
            <span class="n">maskspec</span> <span class="o">=</span> <span class="n">spec</span><span class="p">[</span><span class="n">maskspecnum</span><span class="p">,:]</span>
        <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">specnum</span><span class="p">,</span><span class="nb">list</span><span class="p">):</span>
            <span class="n">spec</span> <span class="o">=</span> <span class="n">spec</span><span class="p">[</span><span class="n">specnum</span><span class="p">,:]</span><span class="o">.</span><span class="n">mean</span><span class="p">(</span><span class="n">axis</span><span class="o">=</span><span class="mi">0</span><span class="p">)</span>
        <span class="k">elif</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">specnum</span><span class="p">,</span><span class="nb">int</span><span class="p">):</span>
            <span class="n">spec</span> <span class="o">=</span> <span class="n">spec</span><span class="p">[</span><span class="n">specnum</span><span class="p">,:]</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">TypeError</span><span class="p">(</span><span class="s">&quot;Specnum is of wrong type (not a list of integers or an integer).  Type: </span><span class="si">%s</span><span class="s">&quot;</span> <span class="o">%</span>
                    <span class="nb">str</span><span class="p">(</span><span class="nb">type</span><span class="p">(</span><span class="n">specnum</span><span class="p">)))</span>
    <span class="k">elif</span> <span class="n">hdr</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s">&#39;NAXIS&#39;</span><span class="p">)</span> <span class="o">&gt;</span> <span class="mi">2</span><span class="p">:</span>
        <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s">&quot;Too many axes for open_1d (splat_1d) - use cube instead&quot;</span><span class="p">)</span>
    <span class="k">if</span> <span class="n">hdr</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s">&#39;CD1_1&#39;</span><span class="o">+</span><span class="n">wcstype</span><span class="p">):</span>
        <span class="n">dv</span><span class="p">,</span><span class="n">v0</span><span class="p">,</span><span class="n">p3</span> <span class="o">=</span> <span class="n">hdr</span><span class="p">[</span><span class="s">&#39;CD1_1&#39;</span><span class="o">+</span><span class="n">wcstype</span><span class="p">],</span><span class="n">hdr</span><span class="p">[</span><span class="s">&#39;CRVAL1&#39;</span><span class="o">+</span><span class="n">wcstype</span><span class="p">],</span><span class="n">hdr</span><span class="p">[</span><span class="s">&#39;CRPIX1&#39;</span><span class="o">+</span><span class="n">wcstype</span><span class="p">]</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="n">dv</span><span class="p">,</span><span class="n">v0</span><span class="p">,</span><span class="n">p3</span> <span class="o">=</span> <span class="n">hdr</span><span class="p">[</span><span class="s">&#39;CDELT1&#39;</span><span class="o">+</span><span class="n">wcstype</span><span class="p">],</span><span class="n">hdr</span><span class="p">[</span><span class="s">&#39;CRVAL1&#39;</span><span class="o">+</span><span class="n">wcstype</span><span class="p">],</span><span class="n">hdr</span><span class="p">[</span><span class="s">&#39;CRPIX1&#39;</span><span class="o">+</span><span class="n">wcstype</span><span class="p">]</span>
    <span class="k">if</span> <span class="n">hdr</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s">&#39;OBJECT&#39;</span><span class="p">):</span>
        <span class="n">specname</span> <span class="o">=</span> <span class="n">hdr</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s">&#39;OBJECT&#39;</span><span class="p">)</span>
    <span class="k">elif</span> <span class="n">hdr</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s">&#39;GLON&#39;</span><span class="p">)</span> <span class="ow">and</span> <span class="n">hdr</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s">&#39;GLAT&#39;</span><span class="p">):</span>
        <span class="n">specname</span> <span class="o">=</span> <span class="s">&quot;</span><span class="si">%s</span><span class="s"> </span><span class="si">%s</span><span class="s">&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="n">hdr</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s">&#39;GLON&#39;</span><span class="p">),</span><span class="n">hdr</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s">&#39;GLAT&#39;</span><span class="p">))</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="n">specname</span> <span class="o">=</span> <span class="n">filename</span><span class="o">.</span><span class="n">rstrip</span><span class="p">(</span><span class="s">&quot;.fits&quot;</span><span class="p">)</span>
    <span class="k">if</span> <span class="n">hdr</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s">&#39;CUNIT1&#39;</span><span class="o">+</span><span class="n">wcstype</span><span class="p">)</span> <span class="ow">in</span> <span class="p">[</span><span class="s">&#39;m/s&#39;</span><span class="p">,</span><span class="s">&#39;M/S&#39;</span><span class="p">]:</span>
        <span class="n">conversion_factor</span> <span class="o">=</span> <span class="mf">1000.0</span>
        <span class="n">xunits</span> <span class="o">=</span> <span class="s">&#39;km/s&#39;</span> <span class="c"># change to km/s because you&#39;re converting units</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="n">xunits</span> <span class="o">=</span> <span class="n">hdr</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s">&#39;CUNIT1&#39;</span><span class="o">+</span><span class="n">wcstype</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">xunits</span> <span class="ow">in</span> <span class="p">(</span><span class="s">&quot;hz&quot;</span><span class="p">,</span><span class="s">&quot;Hz&quot;</span><span class="p">):</span>
            <span class="k">print</span> <span class="s">&quot;Converting from Hz to GHz&quot;</span>
            <span class="n">xunits</span> <span class="o">=</span> <span class="s">&quot;GHz&quot;</span>
            <span class="n">conversion_factor</span> <span class="o">=</span> <span class="mf">1.0e9</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">conversion_factor</span> <span class="o">=</span> <span class="mf">1.0</span>
    <span class="n">vconv</span> <span class="o">=</span> <span class="k">lambda</span> <span class="n">v</span><span class="p">:</span> <span class="p">((</span><span class="n">v</span><span class="o">-</span><span class="n">p3</span><span class="o">+</span><span class="mi">1</span><span class="p">)</span><span class="o">*</span><span class="n">dv</span><span class="o">+</span><span class="n">v0</span><span class="p">)</span><span class="o">/</span><span class="n">conversion_factor</span>
    <span class="n">xtora</span><span class="o">=</span><span class="bp">None</span>
    <span class="n">ytodec</span><span class="o">=</span><span class="bp">None</span>
    <span class="n">units</span> <span class="o">=</span> <span class="n">hdr</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s">&#39;BUNIT&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">strip</span><span class="p">()</span>
    <span class="k">if</span> <span class="n">hdr</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s">&#39;CTYPE1&#39;</span><span class="o">+</span><span class="n">wcstype</span><span class="p">):</span>
        <span class="n">xtype</span> <span class="o">=</span> <span class="n">hdr</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s">&#39;CTYPE1&#39;</span><span class="o">+</span><span class="n">wcstype</span><span class="p">)</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="n">xtype</span> <span class="o">=</span> <span class="s">&#39;VLSR&#39;</span>
    <span class="k">if</span> <span class="n">hdr</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s">&#39;REFFREQ&#39;</span><span class="o">+</span><span class="n">wcstype</span><span class="p">):</span>
        <span class="n">reffreq</span> <span class="o">=</span> <span class="n">hdr</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s">&#39;REFFREQ&#39;</span><span class="o">+</span><span class="n">wcstype</span><span class="p">)</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="n">reffreq</span> <span class="o">=</span> <span class="bp">None</span>

    <span class="k">return</span> <span class="n">dv</span><span class="p">,</span><span class="n">v0</span><span class="p">,</span><span class="n">p3</span><span class="p">,</span><span class="n">conversion_factor</span><span class="p">,</span><span class="n">hdr</span><span class="p">,</span><span class="n">spec</span><span class="p">,</span><span class="n">vconv</span><span class="p">,</span><span class="n">xtora</span><span class="p">,</span><span class="n">ytodec</span><span class="p">,</span><span class="n">specname</span><span class="p">,</span><span class="n">units</span><span class="p">,</span><span class="n">xunits</span><span class="p">,</span><span class="n">errspec</span><span class="p">,</span><span class="n">maskspec</span><span class="p">,</span><span class="n">reffreq</span>
</div>
<div class="viewcode-block" id="splat_1d"><a class="viewcode-back" href="../../agpy.html#agpy.showspec.splat_1d">[docs]</a><span class="k">def</span> <span class="nf">splat_1d</span><span class="p">(</span><span class="n">filename</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span><span class="n">vmin</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span><span class="n">vmax</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span><span class="n">button</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span><span class="n">dobaseline</span><span class="o">=</span><span class="bp">False</span><span class="p">,</span>
        <span class="n">exclude</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span><span class="n">smooth</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span><span class="n">order</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span><span class="n">savepre</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span><span class="n">vcrop</span><span class="o">=</span><span class="bp">True</span><span class="p">,</span>
        <span class="n">vconv</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span><span class="n">vpars</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span><span class="n">hdr</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span><span class="n">spec</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span><span class="n">xtora</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span><span class="n">ytodec</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span>
        <span class="n">specname</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span><span class="n">quiet</span><span class="o">=</span><span class="bp">True</span><span class="p">,</span><span class="n">specnum</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span><span class="n">errspecnum</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span><span class="n">wcstype</span><span class="o">=</span><span class="s">&#39;&#39;</span><span class="p">,</span>
        <span class="n">offset</span><span class="o">=</span><span class="mf">0.0</span><span class="p">,</span> <span class="n">continuum</span><span class="o">=</span><span class="mf">0.0</span><span class="p">,</span> <span class="n">annotatebaseline</span><span class="o">=</span><span class="bp">False</span><span class="p">,</span> <span class="n">plotspectrum</span><span class="o">=</span><span class="bp">True</span><span class="p">,</span>
        <span class="n">smoothto</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="n">xunits</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="n">units</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="n">conversion_factor</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span>
        <span class="n">smoothtype</span><span class="o">=</span><span class="s">&#39;gaussian&#39;</span><span class="p">,</span><span class="n">convmode</span><span class="o">=</span><span class="s">&#39;valid&#39;</span><span class="p">,</span><span class="n">maskspecnum</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span><span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Wrapper for specplotter creation.  Works nicely with 1D spectra with well-defined</span>
<span class="sd">    FITS headers (i.e., CRVAL1, CRPIX1, CDELT1, and optionally CUNIT1 and CTYPE1)</span>

<span class="sd">    This documentation needs to be updated a lot... I implemented a lot of features</span>
<span class="sd">    without documenting them, which was a mistake</span>

<span class="sd">    Inputs:</span>
<span class="sd">        vmin,vmax - range over which to baseline and plot</span>
<span class="sd">        exclude - (internal) range to exclude from baseline fit</span>
<span class="sd">        vcrop - will vmin/vmax crop out data, or just set the plot limits?</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">if</span> <span class="p">(</span><span class="n">vpars</span> <span class="ow">and</span> <span class="n">vconv</span> <span class="ow">and</span> <span class="n">hdr</span> <span class="ow">and</span> <span class="n">spec</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">None</span> <span class="ow">and</span> <span class="n">xtora</span> <span class="ow">and</span> <span class="n">ytodec</span> 
            <span class="ow">and</span> <span class="n">units</span> <span class="ow">and</span> <span class="n">xunits</span> <span class="ow">and</span> <span class="n">conversion_factor</span><span class="p">):</span>
        <span class="n">dv</span><span class="p">,</span><span class="n">v0</span><span class="p">,</span><span class="n">p3</span> <span class="o">=</span> <span class="n">vpars</span>
        <span class="n">errspec</span> <span class="o">=</span> <span class="bp">None</span>
        <span class="n">maskspec</span> <span class="o">=</span> <span class="bp">None</span>
        <span class="n">reffreq</span> <span class="o">=</span> <span class="bp">None</span>
        <span class="k">if</span> <span class="n">units</span> <span class="ow">is</span> <span class="bp">None</span> <span class="ow">and</span> <span class="n">kwargs</span><span class="o">.</span><span class="n">has_key</span><span class="p">(</span><span class="s">&#39;units&#39;</span><span class="p">):</span> <span class="n">units</span> <span class="o">=</span> <span class="n">kwargs</span><span class="o">.</span><span class="n">pop</span><span class="p">(</span><span class="s">&#39;units&#39;</span><span class="p">)</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="n">dv</span><span class="p">,</span><span class="n">v0</span><span class="p">,</span><span class="n">p3</span><span class="p">,</span><span class="n">conversion_factor</span><span class="p">,</span><span class="n">hdr</span><span class="p">,</span><span class="n">spec</span><span class="p">,</span><span class="n">vconv</span><span class="p">,</span><span class="n">xtora</span><span class="p">,</span><span class="n">ytodec</span><span class="p">,</span><span class="n">specname_file</span><span class="p">,</span><span class="n">units</span><span class="p">,</span><span class="n">xunits</span><span class="p">,</span><span class="n">errspec</span><span class="p">,</span><span class="n">maskspec</span><span class="p">,</span><span class="n">reffreq</span> <span class="o">=</span> \
                <span class="n">open_1d</span><span class="p">(</span><span class="n">filename</span><span class="p">,</span><span class="n">specnum</span><span class="o">=</span><span class="n">specnum</span><span class="p">,</span><span class="n">wcstype</span><span class="o">=</span><span class="n">wcstype</span><span class="p">,</span><span class="n">errspecnum</span><span class="o">=</span><span class="n">errspecnum</span><span class="p">,</span><span class="n">maskspecnum</span><span class="o">=</span><span class="n">maskspecnum</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">specname</span> <span class="ow">is</span> <span class="bp">None</span><span class="p">:</span> <span class="n">specname</span><span class="o">=</span><span class="n">specname_file</span>
        <span class="k">if</span> <span class="n">units</span> <span class="ow">is</span> <span class="bp">None</span> <span class="ow">and</span> <span class="n">kwargs</span><span class="o">.</span><span class="n">has_key</span><span class="p">(</span><span class="s">&#39;units&#39;</span><span class="p">):</span> <span class="n">units</span> <span class="o">=</span> <span class="n">kwargs</span><span class="o">.</span><span class="n">pop</span><span class="p">(</span><span class="s">&#39;units&#39;</span><span class="p">)</span>

    <span class="k">if</span> <span class="nb">type</span><span class="p">(</span><span class="n">continuum</span><span class="p">)</span><span class="o">==</span><span class="nb">type</span><span class="p">(</span><span class="s">&#39;str&#39;</span><span class="p">):</span>
        <span class="k">if</span> <span class="n">hdr</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">continuum</span><span class="p">)</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">None</span><span class="p">:</span>
            <span class="n">continuum</span> <span class="o">=</span> <span class="n">hdr</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">continuum</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s">&quot;Continuum specified but none present.&quot;</span><span class="p">)</span>

    <span class="n">varr</span> <span class="o">=</span> <span class="n">vconv</span><span class="p">(</span><span class="n">arange</span><span class="p">(</span><span class="n">spec</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">0</span><span class="p">]))</span>
    <span class="k">if</span> <span class="n">vmin</span> <span class="ow">is</span> <span class="bp">None</span> <span class="ow">or</span> <span class="n">vcrop</span><span class="o">==</span><span class="bp">False</span><span class="p">:</span> <span class="n">argvmin</span> <span class="o">=</span> <span class="mi">0</span>
    <span class="k">else</span><span class="p">:</span> 
      <span class="n">argvmin</span> <span class="o">=</span> <span class="n">argmin</span><span class="p">(</span><span class="nb">abs</span><span class="p">(</span><span class="n">varr</span><span class="o">-</span><span class="n">vmin</span><span class="p">))</span>
      <span class="k">if</span> <span class="n">dv</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">:</span>
        <span class="n">hdr</span><span class="o">.</span><span class="n">update</span><span class="p">(</span><span class="s">&#39;CRPIX1&#39;</span><span class="o">+</span><span class="n">wcstype</span><span class="p">,</span><span class="n">p3</span><span class="o">-</span><span class="n">argvmin</span><span class="p">)</span>
    <span class="k">if</span> <span class="n">vmax</span> <span class="ow">is</span> <span class="bp">None</span> <span class="ow">or</span> <span class="n">vcrop</span><span class="o">==</span><span class="bp">False</span><span class="p">:</span> <span class="n">argvmax</span> <span class="o">=</span> <span class="n">spec</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
    <span class="k">else</span><span class="p">:</span> 
      <span class="n">argvmax</span> <span class="o">=</span> <span class="n">argmin</span><span class="p">(</span><span class="nb">abs</span><span class="p">(</span><span class="n">varr</span><span class="o">-</span><span class="n">vmax</span><span class="p">))</span>
      <span class="k">if</span> <span class="n">dv</span> <span class="o">&lt;</span> <span class="mi">0</span><span class="p">:</span>
        <span class="n">hdr</span><span class="o">.</span><span class="n">update</span><span class="p">(</span><span class="s">&#39;CRPIX1&#39;</span><span class="o">+</span><span class="n">wcstype</span><span class="p">,</span><span class="n">p3</span><span class="o">-</span><span class="n">argvmax</span><span class="p">)</span>

    <span class="k">if</span> <span class="n">argvmin</span> <span class="o">&gt;</span> <span class="n">argvmax</span><span class="p">:</span>
        <span class="n">argvmin</span><span class="p">,</span><span class="n">argvmax</span> <span class="o">=</span> <span class="n">argvmax</span><span class="p">,</span><span class="n">argvmin</span>
        <span class="c">#if exclude is not None: exclude = exclude[::-1]</span>
    <span class="k">elif</span> <span class="n">argvmin</span> <span class="o">==</span> <span class="n">argvmax</span><span class="p">:</span>
        <span class="k">raise</span> <span class="ne">Exception</span><span class="p">(</span><span class="s">&quot;Error: no data in velocity range </span><span class="si">%g</span><span class="s">:</span><span class="si">%g</span><span class="s"> for source </span><span class="si">%s</span><span class="s">.&quot;</span>
                <span class="o">%</span> <span class="p">(</span><span class="n">vmin</span><span class="p">,</span><span class="n">vmax</span><span class="p">,</span><span class="n">filename</span><span class="p">))</span>

    <span class="c"># these lines were meant to automatically put &quot;exclude&quot; into velocity</span>
    <span class="c"># units; this is now done in the baseline code</span>
    <span class="c">#if exclude is not None:</span>
    <span class="c">#    exclude[0] = argmin(abs(varr-exclude[0]))</span>
    <span class="c">#    exclude[1] = argmin(abs(varr-exclude[1]))</span>
    <span class="c">#    exclude = array(exclude) - argvmin</span>

    <span class="n">vconv</span> <span class="o">=</span> <span class="k">lambda</span> <span class="n">v</span><span class="p">:</span> <span class="p">((</span><span class="n">v</span><span class="o">-</span><span class="n">p3</span><span class="o">+</span><span class="n">argvmin</span><span class="o">+</span><span class="mi">1</span><span class="p">)</span><span class="o">*</span><span class="n">dv</span><span class="o">+</span><span class="n">v0</span><span class="p">)</span> <span class="o">/</span> <span class="n">conversion_factor</span>
    <span class="n">ivconv</span> <span class="o">=</span> <span class="k">lambda</span> <span class="n">V</span><span class="p">:</span> <span class="n">p3</span><span class="o">-</span><span class="mi">1</span><span class="o">-</span><span class="n">argvmin</span><span class="o">+</span><span class="p">(</span><span class="n">V</span><span class="o">*</span><span class="n">conversion_factor</span><span class="o">-</span><span class="n">v0</span><span class="p">)</span><span class="o">/</span><span class="n">dv</span>

    <span class="n">specplot</span> <span class="o">=</span> <span class="n">spec</span><span class="p">[</span><span class="n">argvmin</span><span class="p">:</span><span class="n">argvmax</span><span class="p">]</span>
    <span class="k">if</span> <span class="n">errspec</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">None</span><span class="p">:</span> <span class="n">errspec</span><span class="o">=</span><span class="n">errspec</span><span class="p">[</span><span class="n">argvmin</span><span class="p">:</span><span class="n">argvmax</span><span class="p">]</span>
    <span class="k">if</span> <span class="n">maskspec</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">None</span><span class="p">:</span> <span class="n">maskspec</span><span class="o">=</span><span class="n">maskspec</span><span class="p">[</span><span class="n">argvmin</span><span class="p">:</span><span class="n">argvmax</span><span class="p">]</span>

    <span class="k">if</span> <span class="n">smoothto</span><span class="p">:</span>
        <span class="n">smooth</span> <span class="o">=</span> <span class="nb">abs</span><span class="p">(</span><span class="n">smoothto</span><span class="o">/</span><span class="n">dv</span><span class="p">)</span>

    <span class="k">if</span> <span class="n">smooth</span><span class="p">:</span>
        <span class="n">roundsmooth</span> <span class="o">=</span> <span class="nb">round</span><span class="p">(</span><span class="n">smooth</span><span class="p">)</span> <span class="c"># can only downsample by integers</span>
        <span class="c"># change fitter first</span>
        <span class="k">if</span> <span class="n">smoothtype</span> <span class="o">==</span> <span class="s">&#39;hanning&#39;</span><span class="p">:</span> 
            <span class="n">specplot</span> <span class="o">=</span> <span class="n">convolve</span><span class="p">(</span><span class="n">specplot</span><span class="p">,</span><span class="n">hanning</span><span class="p">(</span><span class="mi">2</span><span class="o">+</span><span class="n">roundsmooth</span><span class="p">)</span><span class="o">/</span><span class="n">hanning</span><span class="p">(</span><span class="mi">2</span><span class="o">+</span><span class="n">roundsmooth</span><span class="p">)</span><span class="o">.</span><span class="n">sum</span><span class="p">(),</span><span class="n">convmode</span><span class="p">)[::</span><span class="n">roundsmooth</span><span class="p">]</span>
            <span class="n">kernsize</span> <span class="o">=</span> <span class="n">smooth</span>
            <span class="n">ones_sameshape</span> <span class="o">=</span> <span class="n">zeros</span><span class="p">(</span><span class="n">smooth</span><span class="o">+</span><span class="mi">2</span><span class="p">)</span>
            <span class="n">ones_sameshape</span><span class="p">[</span><span class="mi">1</span><span class="p">:</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span> <span class="o">=</span> <span class="mi">1</span>
        <span class="k">elif</span> <span class="n">smoothtype</span> <span class="o">==</span> <span class="s">&#39;boxcar&#39;</span><span class="p">:</span>
            <span class="n">specplot</span> <span class="o">=</span> <span class="n">convolve</span><span class="p">(</span><span class="n">specplot</span><span class="p">,</span><span class="n">ones</span><span class="p">(</span><span class="n">roundsmooth</span><span class="p">)</span><span class="o">/</span><span class="nb">float</span><span class="p">(</span><span class="n">roundsmooth</span><span class="p">),</span><span class="n">convmode</span><span class="p">)[::</span><span class="n">roundsmooth</span><span class="p">]</span>
            <span class="n">kernsize</span> <span class="o">=</span> <span class="n">roundsmooth</span>
            <span class="n">ones_sameshape</span> <span class="o">=</span> <span class="n">ones</span><span class="p">(</span><span class="n">roundsmooth</span><span class="p">)</span>
        <span class="k">elif</span> <span class="n">smoothtype</span> <span class="o">==</span> <span class="s">&#39;gaussian&#39;</span><span class="p">:</span>
            <span class="n">speclen</span> <span class="o">=</span> <span class="n">specplot</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
            <span class="n">xkern</span>  <span class="o">=</span> <span class="n">linspace</span><span class="p">(</span><span class="o">-</span><span class="mi">1</span><span class="o">*</span><span class="n">smooth</span><span class="p">,</span><span class="n">smooth</span><span class="p">,</span><span class="n">smooth</span><span class="o">*</span><span class="mi">3</span><span class="p">)</span>
            <span class="n">kernel</span> <span class="o">=</span> <span class="n">exp</span><span class="p">(</span><span class="o">-</span><span class="n">xkern</span><span class="o">**</span><span class="mi">2</span><span class="o">/</span><span class="p">(</span><span class="mi">2</span><span class="o">*</span><span class="p">(</span><span class="n">smooth</span><span class="o">/</span><span class="n">sqrt</span><span class="p">(</span><span class="mi">8</span><span class="o">*</span><span class="n">log</span><span class="p">(</span><span class="mi">2</span><span class="p">)))</span><span class="o">**</span><span class="mi">2</span><span class="p">))</span>
            <span class="n">kernel</span> <span class="o">/=</span> <span class="n">kernel</span><span class="o">.</span><span class="n">sum</span><span class="p">()</span>
            <span class="n">kernsize</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">kernel</span><span class="p">)</span>
            <span class="n">specplot</span> <span class="o">=</span> <span class="n">convolve</span><span class="p">(</span><span class="n">specplot</span><span class="p">,</span><span class="n">kernel</span><span class="p">,</span><span class="n">convmode</span><span class="p">)[::</span><span class="n">roundsmooth</span><span class="p">]</span> 
            <span class="n">ones_sameshape</span> <span class="o">=</span> <span class="n">zeros</span><span class="p">(</span><span class="n">roundsmooth</span><span class="o">*</span><span class="mi">3</span><span class="p">)</span>
            <span class="n">ones_sameshape</span><span class="p">[</span><span class="n">roundsmooth</span><span class="p">:</span><span class="o">-</span><span class="n">roundsmooth</span><span class="p">]</span> <span class="o">=</span> <span class="mi">1</span>
        <span class="k">if</span> <span class="n">errspec</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">None</span><span class="p">:</span> 
            <span class="n">errspec</span> <span class="o">=</span> <span class="n">sqrt</span><span class="p">(</span><span class="n">convolve</span><span class="p">(</span><span class="n">errspec</span><span class="o">**</span><span class="mi">2</span><span class="p">,</span><span class="n">ones_sameshape</span><span class="p">,</span><span class="n">convmode</span><span class="p">)[::</span><span class="n">roundsmooth</span><span class="p">])</span> <span class="o">/</span> <span class="nb">float</span><span class="p">(</span><span class="n">roundsmooth</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">maskspec</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">None</span><span class="p">:</span> 
            <span class="n">maskspec</span> <span class="o">=</span> <span class="n">array</span><span class="p">(</span><span class="n">convolve</span><span class="p">(</span><span class="n">maskspec</span><span class="p">,</span><span class="n">ones_sameshape</span><span class="p">,</span><span class="n">convmode</span><span class="p">)[::</span><span class="n">roundsmooth</span><span class="p">],</span><span class="n">dtype</span><span class="o">=</span><span class="s">&#39;bool&#39;</span><span class="p">)</span>
            <span class="k">if</span> <span class="n">maskspec</span><span class="o">.</span><span class="n">shape</span> <span class="o">!=</span> <span class="n">specplot</span><span class="o">.</span><span class="n">shape</span><span class="p">:</span> <span class="kn">import</span> <span class="nn">pdb</span><span class="p">;</span> <span class="n">pdb</span><span class="o">.</span><span class="n">set_trace</span><span class="p">()</span>
        <span class="c"># this bit of code may also make sense, but I&#39;m shifting the center pixel instead</span>
        <span class="c"># b/c it&#39;s easier (?) to deal with velocity range</span>
        <span class="c">#v0 += (abs(dv)*smooth - abs(dv))/2.0 # pixel center moves by half the original pixel size</span>
        <span class="n">dv</span> <span class="o">*=</span> <span class="n">roundsmooth</span>
        <span class="k">if</span> <span class="n">convmode</span> <span class="o">==</span> <span class="s">&#39;same&#39;</span><span class="p">:</span>
          <span class="n">newrefpix</span> <span class="o">=</span> <span class="p">(</span><span class="n">p3</span><span class="o">-</span><span class="n">argvmin</span><span class="p">)</span><span class="o">/</span><span class="n">roundsmooth</span>  
        <span class="k">elif</span> <span class="n">convmode</span> <span class="o">==</span> <span class="s">&#39;full&#39;</span><span class="p">:</span>
          <span class="n">newrefpix</span> <span class="o">=</span> <span class="p">(</span><span class="n">p3</span><span class="o">-</span><span class="mf">0.5</span><span class="o">-</span><span class="n">argvmin</span><span class="o">+</span><span class="n">kernsize</span><span class="o">/</span><span class="mf">2.0</span><span class="p">)</span><span class="o">/</span><span class="n">roundsmooth</span>  
        <span class="k">elif</span> <span class="n">convmode</span> <span class="o">==</span> <span class="s">&#39;valid&#39;</span><span class="p">:</span>
          <span class="n">newrefpix</span> <span class="o">=</span> <span class="p">(</span><span class="n">p3</span><span class="o">-</span><span class="mf">0.5</span><span class="o">-</span><span class="n">argvmin</span><span class="o">-</span><span class="n">kernsize</span><span class="o">/</span><span class="mf">2.0</span><span class="p">)</span><span class="o">/</span><span class="n">roundsmooth</span>  
        <span class="c"># this was resolved by advanced guess-and check</span>
        <span class="c"># but also, sort of makes sense: FITS refers to the *center* of a pixel.  You want to </span>
        <span class="c"># shift 1/2 pixel to the right so that the first pixel goes from 0 to 1</span>
        <span class="n">vconv</span> <span class="o">=</span> <span class="k">lambda</span> <span class="n">v</span><span class="p">:</span> <span class="p">((</span><span class="n">v</span><span class="o">-</span><span class="n">newrefpix</span><span class="p">)</span><span class="o">*</span><span class="n">dv</span><span class="o">+</span><span class="n">v0</span><span class="p">)</span><span class="o">/</span><span class="n">conversion_factor</span>
        <span class="n">ivconv</span> <span class="o">=</span> <span class="k">lambda</span> <span class="n">V</span><span class="p">:</span> <span class="n">newrefpix</span><span class="o">+</span><span class="p">(</span><span class="n">V</span><span class="o">*</span><span class="n">conversion_factor</span><span class="o">-</span><span class="n">v0</span><span class="p">)</span><span class="o">/</span><span class="n">dv</span>
        <span class="n">hdr</span><span class="o">.</span><span class="n">update</span><span class="p">(</span><span class="s">&#39;CRPIX1&#39;</span><span class="o">+</span><span class="n">wcstype</span><span class="p">,</span><span class="n">newrefpix</span><span class="o">+</span><span class="mi">1</span><span class="p">)</span>
        <span class="n">hdr</span><span class="o">.</span><span class="n">update</span><span class="p">(</span><span class="s">&#39;CDELT1&#39;</span><span class="o">+</span><span class="n">wcstype</span><span class="p">,</span><span class="n">dv</span><span class="p">)</span>

    <span class="n">sp</span> <span class="o">=</span> <span class="n">SpecPlotter</span><span class="p">(</span><span class="n">specplot</span><span class="p">,</span> <span class="n">vconv</span><span class="o">=</span><span class="n">vconv</span><span class="p">,</span> <span class="n">xtora</span><span class="o">=</span><span class="n">xtora</span><span class="p">,</span> <span class="n">ytodec</span><span class="o">=</span><span class="n">ytodec</span><span class="p">,</span>
            <span class="n">specname</span><span class="o">=</span><span class="n">specname</span><span class="p">,</span> <span class="n">dv</span><span class="o">=</span><span class="n">dv</span><span class="o">/</span><span class="n">conversion_factor</span><span class="p">,</span> <span class="n">hdr</span><span class="o">=</span><span class="n">hdr</span><span class="p">,</span> <span class="n">reffreq</span><span class="o">=</span><span class="n">reffreq</span><span class="p">,</span>
            <span class="n">errspec</span><span class="o">=</span><span class="n">errspec</span><span class="p">,</span> <span class="n">maskspec</span><span class="o">=</span><span class="n">maskspec</span><span class="p">,</span> <span class="n">xunits</span><span class="o">=</span><span class="n">xunits</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>

    <span class="k">if</span> <span class="n">plotspectrum</span><span class="p">:</span>
        <span class="n">sp</span><span class="o">.</span><span class="n">plotspec</span><span class="p">(</span><span class="n">button</span><span class="o">=</span><span class="n">button</span><span class="p">,</span> <span class="n">cube</span><span class="o">=</span><span class="bp">False</span><span class="p">,</span> <span class="n">vmin</span><span class="o">=</span><span class="n">vmin</span><span class="p">,</span> <span class="n">vmax</span><span class="o">=</span><span class="n">vmax</span><span class="p">,</span>
                <span class="n">units</span><span class="o">=</span><span class="n">units</span><span class="p">,</span> <span class="n">offset</span><span class="o">=</span><span class="n">offset</span><span class="p">,</span> <span class="n">continuum</span><span class="o">=</span><span class="n">continuum</span><span class="p">,</span>
                <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>

    <span class="k">if</span> <span class="n">dobaseline</span><span class="p">:</span> 
        <span class="n">sp</span><span class="o">.</span><span class="n">baseline</span><span class="p">(</span><span class="n">exclude</span><span class="o">=</span><span class="n">exclude</span><span class="p">,</span><span class="n">order</span><span class="o">=</span><span class="n">order</span><span class="p">,</span><span class="n">quiet</span><span class="o">=</span><span class="n">quiet</span><span class="p">,</span><span class="n">annotate</span><span class="o">=</span><span class="n">annotatebaseline</span><span class="p">)</span>
    <span class="k">if</span> <span class="n">plotspectrum</span><span class="p">:</span> <span class="n">sp</span><span class="o">.</span><span class="n">refresh</span><span class="p">()</span>
    
    <span class="k">if</span> <span class="n">hdr</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s">&#39;GLON&#39;</span><span class="p">)</span> <span class="ow">and</span> <span class="n">hdr</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s">&#39;GLAT&#39;</span><span class="p">):</span>
        <span class="n">sp</span><span class="o">.</span><span class="n">glon</span> <span class="o">=</span> <span class="n">hdr</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s">&#39;GLON&#39;</span><span class="p">)</span>
        <span class="n">sp</span><span class="o">.</span><span class="n">glat</span> <span class="o">=</span> <span class="n">hdr</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s">&#39;GLAT&#39;</span><span class="p">)</span>

    <span class="k">if</span> <span class="n">savepre</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">None</span><span class="p">:</span>
        <span class="n">glon</span><span class="p">,</span><span class="n">glat</span> <span class="o">=</span> <span class="n">sp</span><span class="o">.</span><span class="n">glon</span><span class="p">,</span><span class="n">sp</span><span class="o">.</span><span class="n">glat</span>
        <span class="k">if</span> <span class="n">glat</span> <span class="o">&lt;</span> <span class="mi">0</span><span class="p">:</span> <span class="n">pm</span><span class="o">=</span><span class="s">&quot;&quot;</span> 
        <span class="k">else</span><span class="p">:</span> <span class="n">pm</span> <span class="o">=</span> <span class="s">&quot;+&quot;</span>
        <span class="n">savename</span> <span class="o">=</span> <span class="n">savepre</span> <span class="o">+</span> <span class="s">&quot;G</span><span class="si">%07.3f%0s%07.3f</span><span class="s">_&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="n">glon</span><span class="p">,</span><span class="n">pm</span><span class="p">,</span><span class="n">glat</span><span class="p">)</span> <span class="o">+</span> <span class="n">hdr</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s">&#39;MOLECULE&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="s">&#39; &#39;</span><span class="p">,</span><span class="s">&#39;&#39;</span><span class="p">)</span> <span class="o">+</span> <span class="n">hdr</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s">&#39;TRANSITI&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="s">&#39; &#39;</span><span class="p">,</span><span class="s">&#39;&#39;</span><span class="p">)</span>
        <span class="n">savefig</span><span class="p">(</span><span class="n">savename</span><span class="o">+</span><span class="s">&#39;.png&#39;</span><span class="p">)</span>

    <span class="k">return</span> <span class="n">sp</span>
</div>
<div class="viewcode-block" id="splat_tspec"><a class="viewcode-back" href="../../agpy.html#agpy.showspec.splat_tspec">[docs]</a><span class="k">def</span> <span class="nf">splat_tspec</span><span class="p">(</span><span class="n">filename</span><span class="p">,</span><span class="n">specnum</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span><span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Same as splat_1d for tspec data</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="n">tdata</span> <span class="o">=</span> <span class="n">pyfits</span><span class="o">.</span><span class="n">getdata</span><span class="p">(</span><span class="n">filename</span><span class="p">)</span>
    <span class="n">theader</span> <span class="o">=</span> <span class="n">pyfits</span><span class="o">.</span><span class="n">getheader</span><span class="p">(</span><span class="n">filename</span><span class="p">)</span>
    <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">tdata</span><span class="o">.</span><span class="n">shape</span><span class="p">)</span> <span class="o">==</span> <span class="mi">3</span><span class="p">:</span>
        <span class="n">tdata</span> <span class="o">=</span> <span class="n">tdata</span><span class="p">[</span><span class="n">specnum</span><span class="p">,:,:]</span>
    <span class="n">wavelength</span> <span class="o">=</span> <span class="n">tdata</span><span class="p">[</span><span class="mi">0</span><span class="p">,:]</span>
    <span class="n">spectrum</span>   <span class="o">=</span> <span class="n">tdata</span><span class="p">[</span><span class="mi">1</span><span class="p">,:]</span>
    <span class="n">error</span>      <span class="o">=</span> <span class="n">tdata</span><span class="p">[</span><span class="mi">2</span><span class="p">,:]</span>

    <span class="n">vconv</span> <span class="o">=</span> <span class="k">lambda</span> <span class="n">x</span><span class="p">:</span> <span class="n">wavelength</span><span class="p">[</span><span class="n">x</span><span class="p">]</span>
    <span class="n">ivconv</span> <span class="o">=</span> <span class="k">lambda</span> <span class="n">x</span><span class="p">:</span> <span class="n">argmin</span><span class="p">(</span><span class="nb">abs</span><span class="p">(</span><span class="n">wavelength</span><span class="o">-</span><span class="n">x</span><span class="p">))</span>
    
    <span class="n">specname</span><span class="o">=</span><span class="s">&#39;TSPEC&#39;</span>
    <span class="n">dv</span> <span class="o">=</span> <span class="n">median</span><span class="p">(</span><span class="n">wavelength</span><span class="p">[</span><span class="mi">1</span><span class="p">:]</span> <span class="o">-</span> <span class="n">wavelength</span><span class="p">[:</span><span class="o">-</span><span class="mi">1</span><span class="p">])</span>
    
    <span class="n">sp</span> <span class="o">=</span> <span class="n">SpecPlotter</span><span class="p">(</span><span class="n">spectrum</span><span class="p">,</span><span class="n">vconv</span><span class="o">=</span><span class="n">vconv</span><span class="p">,</span><span class="n">specname</span><span class="o">=</span><span class="n">specname</span><span class="p">,</span><span class="n">dv</span><span class="o">=</span><span class="n">dv</span><span class="p">,</span><span class="n">hdr</span><span class="o">=</span><span class="n">theader</span><span class="p">)</span>

    <span class="n">sp</span><span class="o">.</span><span class="n">plotspec</span><span class="p">(</span><span class="n">cube</span><span class="o">=</span><span class="bp">False</span><span class="p">,</span><span class="n">units</span><span class="o">=</span><span class="n">theader</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s">&#39;YUNITS&#39;</span><span class="p">),</span><span class="n">xunits</span><span class="o">=</span><span class="n">theader</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s">&#39;XUNITS&#39;</span><span class="p">),</span><span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>

    <span class="k">return</span> <span class="n">sp</span></div>
</pre></div>

          </div>
        </div>
      </div>
        </div>
        <div class="sidebar">
          <h3>Table Of Contents</h3>
          <ul>
<li class="toctree-l1"><a class="reference internal" href="../../agpy.html">Adam Ginsburg&#8217;s Python Code (agpy)</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../image_tools.html">Image Tools</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../fft_tools.html">AG_fft_tools Package</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../plfit.html">plfit Package</a></li>
</ul>

          <h3 style="margin-top: 1.5em;">Search</h3>
          <form class="search" action="../../search.html" method="get">
            <input type="text" name="q" />
            <input type="submit" value="Go" />
            <input type="hidden" name="check_keywords" value="yes" />
            <input type="hidden" name="area" value="default" />
          </form>
          <p class="searchtip" style="font-size: 90%">
            Enter search terms or a module, class or function name.
          </p>
        </div>
        <div class="clearer"></div>
      </div>
    </div>

    <div class="footer">
      &copy; Copyright 2011, Adam Ginsburg.
      Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.2pre.
    <script type="text/javascript">

      var _gaq = _gaq || [];
      _gaq.push(['_setAccount', 'UA-6253248-2']);
      _gaq.push(['_trackPageview']);

      (function() {
        var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
        ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
        var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
      })();

    </script>
        
    </div>
  </body>
</html>